Static and non-static access to method
package PackageB;
publicclass SubclassB
{
static SubclassB ref=new SubclassB();;
publicstaticvoid main(String[] args)
{
ref.func();
}
publicvoid func()
{
System.out.println("Function invoked");
}
}
When i compile this code snippet it prints "Function invoked". But how is this possible? How can ref have access to a non-static function ( func() )?
[957 byte] By [
ripper079a] at [2007-11-27 8:28:18]

Non-static methods are applied to objects. If you couldn't apply non-static methods to ref, what good would it be?
hey ripper,
i dont know exactly... but it seems that when you are invoking
java PackageB.SubClassB
the class loader first loads the class and it notice that the class itself has a static reference to an object of its own class, so the class loader is creating an object of SubclassB and storing the reference in "ref"...
now control returns to main() and it just invokes func() of the static object "ref" which is actually created by java run time...
lets see what the experts has to say about this...
regards
i_virus
ref is a reference to an instance of SubclassB. This is exactly the case when you CAN access non-static methods. That's why they're called instance methods.
Let's say you couldn't call ref.func. Can you come up with a scenario that's consistent with what you imagine the rules to be where a non-static method could ever be invoked?
jverda at 2007-7-12 20:18:15 >

> hey ripper,
>
> i dont know exactly... but it seems that when you are
> invoking
>
> java PackageB.SubClassB
>
> the class loader first loads the class and it notice
> that the class itself has a static reference to an
> object of its own class,
No, it doesn't. It simply executes the static variable declaration and initialization. It doesn't notice anything special about the fact that it's a reference to an instance of its own class.
jverda at 2007-7-12 20:18:15 >

hey jverd,
i dont wanted to mean thats something is special here... i just wanted to picture the underlying process thats goiing on...
perhaps this is what i mean to say...
1. the class loader loads the class
2. it creates an object of SubclassB and assign it to variable "ref"
3. control return to main()
4. main simply invokes func() through ref
please correct me if this is not what happening...
regards
i_virus
> 1. the class loader loads the class
> 2. it creates an object of SubclassB and assign it to
> variable "ref"
It executes whatever static variable initializations and static initializer blocks are there. In this case, it's the one for SubclassB. It would do exactly the same thing if it were a Date or ArrayList or whatever.
> 3. control return to main()
> 4. main simply invokes func() through ref
jverda at 2007-7-12 20:18:15 >

hey jverd,thats exactly what i mean...regradsi_virus
Hmm yes i understand that it is a instance. But the thing that i don磘 understand is e.i what difference is it if i would emit the static modifier
SubclassB ref= new SubclassB();
What does the static declaration exactly? How would the code behave if i would emit it? I can磘 imagine that it would behave the same. In that case what is the purpose with it?
> Hmm yes i understand that it is a instance. But the
> thing that i don磘 understand is e.i what difference
> is it if i would emit the static modifier
> SubclassB ref= new SubclassB();
>
> What does the static declaration exactly? How would
> the code behave if i would emit it? I can磘 imagine
> that it would behave the same. In that case what is
> the purpose with it?
If it's static, there's one ref variable that's associated with the class as a whole. If you change it in any way every instance will see it.
If i's non-static, then each instance has its own copy, and changing one will not change the other, BUT, in this case, since it's an instance of that class itself, every time you create an instance with new SubClassB(), that line will be executed to cause that instance to have a reference to another instance, which will in turn get a reference to another instance, and so on to infinity or until your stack vomits, whichever happens first.
If you're going to have a reference to an instance of the same class in which the variable exists, it has to be static, else you'll get into infinite recursion.
jverda at 2007-7-12 20:18:15 >

