static
members of a class
are class
level Members its means there is no need of Object to access these static
Members.`
It automatically loaded when classloader
of jvm
loads the class. So here in this case
static String name = " JavaQuiz"; //load when class get loaded by JVM class loader.
This static
variable will exists in memory just after class Foo
is getting loaded into the jvm
.
static Foo getFoo() { //method is also a static Member automatically loaded at Class Loading.
System.out.print("Getting Object");
return null;
}
And same will be applied with this static method getFoo()
.
So Here System.out.println(getFoo().name);
.
Here is a example to abbriviate my Answer
class StaticExample
{
static String abc ="India";
public static void main (String[] args) throws java.lang.Exception
{
StaticExample obj = null;
System.out.println("Value is ==>" + obj.abc + StaticExample.abc + abc);
}
}
Output:-
Value is ==>IndiaIndiaIndia
here this line of code will also produce the output.
System.out.println(((Ideone)null).abc); // this will also print India.
Output:-
Value is ==>India
Note:- I think there is confusion about getFoo()
method but if .name
makes a ambiguity. name
is a static member so it can access using either className
or any null
reference . So, Here you may assume this scenario such that this name
variable is accessed with any null reference .
Hope you Got the point.