user2802411
user2802411

Reputation: 31

Why static object of non-static class cannot be created inside a method?

I can see that static object of non-static class cannot be created inside a method?

Code:

  public class Rent {

      public void abc() {
          System.out.println("Non static method");
      }
      public static void def() {
          System.out.println("this is static method");
      }
  }

  public class SampleJava {

      public static void main(String[] args) {
          Rent r1 = new Rent();
          public static Rent r2; //not allowed in static method
      }

      public static Rent r3; //allowed outside method

      public void def() {
          Rent r4 = new Rent();
          public static Rent r5; //not allowed in non-static method either
      }
  }

Upvotes: 2

Views: 2837

Answers (4)

user3437460
user3437460

Reputation: 17454

First of all you are not allowed to declare variables with access modifiers(public, private, protected) within a method. You are also not supposed to declare variables as static within a method.

Those variables you declared outside the methods are members of a class.

There are 2 types of variables:

  • Instance variables (non-static members)
  • Class variables (static members)

I can see that static object of non-static class cannot be created inside a method?

static means it belongs to the class. It exist even before you instantiate the object. That is why you could invoke a static method through the class directly.

So what are static members?

  • static members are shared by all objects from the same class.
  • it exist even before you instantiate (create) an object.
  • it belongs to the class, and not any individual object.

Example:

class Pond
{
    static double waterTemp = 17.5;  //Every Pond object shares the same value
    int numFishes;                   //Belongs to each individual Pond object
}

How to access static members:

System.out.println(Pond.waterTemp); //Preferred
System.out.println(Pond.numFished); //Not allowed

//OR

Pond pond = new Pond();  
System.out.println(pond.waterTemp); //Not necessary, but allowed
System.out.println(pond.numFished); //The only way to get numFishes

Upvotes: 1

Nakul91
Nakul91

Reputation: 1245

There are few points you have to consider:

  1. Static Data is similar to a Static Method. It has nothing to do with the instance. A value that is declared static has no associated instance. It exists for every instance, and is only declared in a single place in memory. If it ever gets changed, it will change for every instance of that class.

  2. The access modifiers you have used are only allowed for class level and not method level. In your example:

    public static void main(String[] args) {
      Rent r1 = new Rent();
      public static Rent r2; //Will give compile time error.
    }  
    
  3. Considering the purpose of "Static", if you are declaring the static object inside the method then its scope will be bind to that particular method only.

Usually the static objects are used to maintain the state.

For e.g. your database connection logic may have a singleton class and the object of this class should keep your state of database connection. These objects needs to be and must be at class level.

Upvotes: 6

Dragan Bozanovic
Dragan Bozanovic

Reputation: 23552

The errors you are getting have nothing to do static/instance access. They are just compile time errors because of invalid Java syntax.

You cannot use field modifiers like public and static for declarations of local variables inside a method (be it a static or an instance method).

Just remove the unnecessary modifiers:

  public static void main(String[] args) {
      Rent r1 = new Rent();
      Rent r2;
  }

  public static Rent r3;

  public void def() {
      Rent r4 = new Rent();
      Rent r5;
  }

Upvotes: 2

anish
anish

Reputation: 492

A non-static method is associated with an instance and hence is required that an instance be created before it can be accessed. However, a static method is related to class and not hence it should be available for use even without an instance. These constraints cause the static method to not be created inside an non-static method.

Upvotes: 0

Related Questions