1

Why static variables are directly called without any use of object in java?Are they stored in some different memory location?And why only static methods can called with the name of the class directly without creating its object ?for example

class First
{
   public static void ss()
   {
     System.out.println("This genius will give answer");
   }
}

class Second
{
    public static void main(String ar[])
    {
       First.ss(); 

    }
}
iCantSeeSharp
  • 3,880
  • 4
  • 42
  • 65
Shikhil Bhalla
  • 392
  • 1
  • 5
  • 17

2 Answers2

1

Yes static resources belong to the class and not the objects. And are stored in a separate location kind of global location. You can read more here.

Community
  • 1
  • 1
rocketboy
  • 9,573
  • 2
  • 34
  • 36
1

As docs says

Every instance of the class shares a class variable, which is in one fixed location in memory.

The Java programming language supports static methods as well as static variables. Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class, as in

ClassName.methodName(args)
Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307