Java Static Method and Constructor in Java

5/5 - (2 votes)

Hello everyone, in the previous topic we were talking about Wrapper Class in Java and Method and Method Overloading. Today we are going to talk about the Java Static Method and Constructor in Java.

Java Static Method and Constructor in Java

Java Static Method and Constructor in Java
Java Static Method and Constructor in Java

Java Static Method

If we apply a static keyword with any method, it is known as the static method.

  • A static method belongs to the class rather than the object of a class.
  • A static method can be invoked without the need for creating an instance of a class.
  • A static method can access static data members and can change the value of it.
Syntax of Static Method
<class-name>.<method-name>
Example of Static Method
class test
{
 static void myMethod( )
{
 System.out.println(“myMethod”);
}
public static void main(String[] args)
{
myMethod();
}
}

Can we overload static methods?

We can overload static methods. We can have two or more static methods with the same name, but differences in input parameters.

public class test {
public static void foo( ) {
 System.out println(“Test foo () called “);
}
public static void foo(int a) {
  System.out.println(“Test.foo(int) called “);
}
 public static void main(String args [])
{
test foo();
test foo(10);
}
Output- Test.foo() called
 Test.foo(int) called

Can we overload methods that differ only by static keyword?

We cannot overload two methods in Java if they differ only by static keyword (number of parameters and types of parameters is same).

public class test{
public static void foo () {
System.out.println(“Test.foo() called “); }
public void foo() {// Compiler Error: cannot redefine foo() System.out.printIn(” test.foo(int) called “); }
public static void main(String args[]){
test.foo(); }
}
Output- Compiler Error, cannot redefine foo()

Restrictions for the static method

There are two main restrictions for the static method. They are-

  • The static method can not use non-static data members or call the non-static method directly.
  • This and super cannot be used in a static context.

Instance method v/s Static method

  1. The instance method can access the instance methods and instance variables directly.
  2. The instance method can access static variables and static methods directly.
  3. Static methods can access the static variables and static methods directly.
  4. Static methods can’t access instance methods and instance variables directly. They must use reference to object. And static method can’t use this keyword as there is no instance for ‘this’ to refer to.

Constructor in Java

A constructor is a special method that is used to initialize a newly created object and is called just after the memory is allocated for the object. It can be used to initialize the objects to desired values or default values at the time of object creation. It is not mandatory for the coder to write a constructor for a class.

If no user-defined constructor is provided for a class, the compiler initializes member variables to their default values. Every time an object is created using the new() keyword, at least one constructor is called. It calls a default constructor if there is no constructor available in the class. In such a case, the Java compiler provides a default constructor by default.

It is called a constructor because it constructs the values at the time of object creation. It is not necessary to write a constructor for a class. It is because the java compiler creates a default constructor if your class doesn’t have any.

If no user-defined constructor is provided for a class, the compiler initializes member variables to their default values.

  • numeric data types are set to 0
  • char data types are set to the null character(\0′)
  • reference variables are set to null

Rules for creating Java constructor

Following are the rules for creating a Java constructor-

  • The constructor name must be the same as its class name.
  • A Constructor must have no explicit return type.
  • A Java constructor cannot be abstract, static, final, and synchronized.
  • Constructors are called implicitly whenever an object is created.
  • It should not return a value not even void.
  • We can use access modifiers while declaring a constructor. It controls object creation. In other words, we can have private, protected, public, or default constructors in Java.

Types of Java constructors

There are two types of constructors in Java-

  1. Default constructor (no-arg constructor)
  2. Parameterized constructor

Java Default Constructor

A constructor is called a “Default Constructor” when it doesn’t have any parameter.

Syntax of default constructor- <class_name> ()
}
Example of Java Default Constructor
class test
{
test()
{
System.out.printIn(“Hello Java Default Constructor”);
}
public static void main(String args(])
{
//calling a default constructor
test t=new test();
}
}

The rule that follows

If there is no constructor in a class, the compiler automatically creates a default constructor.

class test
{
}
Compiler
class test { test () {}

What is the purpose of a default constructor?

The default constructor is used to provide the default values to the object like 0, null, etc., depending on the type.

Example of default constructor that displays the default values is given below
class test{
int id;
String name;
void display() { System.out.printIn(id+” “+name); }
public static void main(String args[]){
test s1=new test();
test s2=new test();
s1.display();
 s2.display();
}
}
Output-
O null
O null

Java Parameterized Constructor

A constructor which has a specific number of parameters is called a parameterized constructor.

Why use the parameterized constructor?

The parameterized constructor is used to provide different values to distinct objects.

Example
class test {
int id;
String name;
test(int i,String n)
{
 id = i;
name=n;
}
 name = n;
}
void display()
{ System.out.println(id+”” +name); }
public static void main(String args []){
test s1 = new test(001,”Ram“);
test s2 = new test(002, “Shayam“);
s1.display();
$2.display();
}
}
Output-
001 Ram
002 Shayam

So, that is all for today guys see you in our next blog. If you like our article please don’t forget to share with others & follow our Instagram page for your daily dose of Motivation.

Thank You,

Regards

Grooming Urban

General FAQ

What is Java Static Method?

If we apply a static keyword with any method, it is known as the java static method.

Can we overload static methods?

We can overload static methods. We can have two or more static methods with the same name, but differences in input parameters.

Can we overload methods that differ only by static keyword?

We cannot overload two methods in Java if they differ only by static keyword (number of parameters and types of parameters is same).

What are the restrictions for the static method?

There are two main restrictions for the static method. They are-
a. The static method can not use non-static data members or call the non-static method directly.
b. This and super cannot be used in a static context.

What is a constructor in Java?

A constructor is a special method that is used to initialize a newly created object and is called just after the memory is allocated for the object. It can be used to initialize the objects to desired values or default values at the time of object creation. It is not mandatory for the coder to write a constructor for a class.

What are the rules for creating a Java constructor?

Following are the rules for creating a Java constructor-
1. The constructor name must be the same as its class name.
2. A Constructor must have no explicit return type.
3. A Java constructor cannot be abstract, static, final, and synchronized.
4. Constructors are called implicitly whenever an object is created.
5. It should not return a value not even void.
6. We can use access modifiers while declaring a constructor. It controls object creation. In other words, we can have private, protected, public, or default constructors in Java.

How many types of Java constructors?

There are two types of constructors in Java-
a. Default constructor (no-arg constructor)
b. Parameterized constructor

What is Default Constructor in Java?

A constructor is called a “Default Constructor” when it doesn’t have any parameter.

What is the purpose of a default constructor?

The default constructor is used to provide the default values to the object like 0, null, etc., depending on the type.

What is Java Parameterized Constructor?

A constructor which has a specific number of parameters is called a parameterized constructor.

Why use the parameterized constructor?

The parameterized constructor is used to provide different values to distinct objects.

Sharing Is Caring:
Sneha Kriti

An aspiring MCA student formed an obsession with Computer Science And Coding with HTML, C, C++, Java, and Python now helping others to improve in their studies, grooming and personality traits as well.

1 thought on “Java Static Method and Constructor in Java”

Leave a Comment