Computer Science, asked by nikhilpatilsh6209, 1 year ago

Why do you make a method static instead of non-static?

Answers

Answered by pushpa2devgmailcom
0
method in Java is a collection of statements with a name or a method name that are grouped together to perform a task. You can execute the group of statements by calling the method by its name.

Suppose you wanted to make a dress from a dress that you already have. You can simply use the original dress to make the new dress. Or, you could make a paper pattern from your original dress and use the paper pattern to create new dresses.

A static method in Java belongs to the class and is like the blue print or the original dress. A non-static method in Java belongs to the object of a class and is like the paper pattern made from the blue print or original dress.

To create a static method in Java, you prefix the key word 'static' before the name of the method. A static method belongs to the class, and you do not have to create an instance of the class to access the static method. Referring to the dress analogy, a static method would get its design from the original dress.

A non-static method in Java does not have the key word 'static' before the name of the method. A non-static method belongs to an object of the class, and you have to create an instance of the class to access the non-static method. From the dress example, a non-static method would get the measurements from a pattern of the original dress.

Characteristics of Static Methods

A static method can access static methods and variables as follows:

A static method can call only other static methods; it cannot call a non-static method

A static method can be called directly from the class, without having to create an instance of the class

A static method can only access static variables; it cannot access instance variables

Since the static method refers to the class, the syntax to call or refer to a static method is: class name.method name

To access a non-static method from a static method, create an instance of the class

Characteristics of Non-Static Methods

A non-static method in Java can access static methods and variables as follows:

A non-static method can access any static method without creating an instance of the class

A non-static method can access any static variable without creating an instance of the class because the static variable belongs to the class

Calling Static & Non-Static Methods

In this code, the static method main calls a static method product:

class Calc {

  static int product(int x, int y) {

   return x * y;

  }

  public static void main(String[] args) {

   int ans = Calc.product(5, 3);

   System.out.println(ans);

  }

}

In this code, this method is a static method:

static int product(int x, int y) {

}

So, it can be called from the main method (which is also another static method) in the following way:

calc.product(5,3);

If we change the method from static to non-static, it would generate an error, since the static method main can only call another static method. So for example, this code would generate an error:

class Calc {

  int product(int x, int y) {

   return x * y;

  }

  public static void main(String[] args) {

   int ans = Calc.product(5, 3);

   System.out.println(ans);

  }

}

This code causes the following error:





The error appears because the method product is not declared as static, and we are trying to access it from a static mainmethod.

Calling a Non-Static Method from a Static Method

If we need to call the non-static method product from the static method main, then we would have to create an instance of class Calc and call the method from the newly created instance. The code below does not generate an error.

class Calc {

  int product(int x, int y) {

   return x * y;

  }

  public static void main(String[] args) {

   Calc calcnew = new Calc();

   // create an instance of the class Calc

   int ans = calcnew.product(5, 3); // call the non-static method

   System.out.println(ans);

  }

}

Here in the method main creates an instance of the class calcnew and the non-static method is then called from the newly created instance of the class.

Calling a Static Variable from a Static Method

In the code excerpt here, the static variable a is called from the static method main by directly referencing the variable.

To unlock this lesson you must be a Study.com Member. 
Create your account

Register to view this lesson

Are you a student or a teacher?

 I am a student I am a teacher

Static vs. Non-Static Methods in JavaRelated Study Materials

Related

Recently Updated

Popular

Explore Subjects

Create an account to start this course today

Try it risk-free for 30 days!

Create An Account

Like this lesson Share

Explore our library of over 75,000 lessons

Search

Browse

Browse by subject


Similar questions