About Question enthuware.ocajp.i.v7.2.1131 :
Moderators: Site Manager, fjwalraven
About Question enthuware.ocajp.i.v7.2.1131 :
I cannot conceptually get how we can have a static reference to a class of the type that it's being defined in. I get the concept of the question but not how 'static TestClass ref;' works.
Can you explain please?
Thanks
Can you explain please?
Thanks
-
- Site Admin
- Posts: 10064
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1131 :
In this case, ref is just a static field in class, which happens to be of type TestClass.
I am just guessing that you are confused because at the time of declaring the class, you dont know the size of the class and so having a member of the same time may cause a recursion type scenario.
But ref is just like a pointer. So it doesn't have to know the space needed to store an object of type TestClass because the size of the pointer is constant. So it can point to any object even if that object is of the same type as the declaring class and even if the type of the object that it is pointing to is not known.
HTH
I am just guessing that you are confused because at the time of declaring the class, you dont know the size of the class and so having a member of the same time may cause a recursion type scenario.
But ref is just like a pointer. So it doesn't have to know the space needed to store an object of type TestClass because the size of the pointer is constant. So it can point to any object even if that object is of the same type as the declaring class and even if the type of the object that it is pointing to is not known.
HTH
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.1131 :
So if you did
static TestClass ref = new TestClass();
would it be an error?
static TestClass ref = new TestClass();
would it be an error?
-
- Site Admin
- Posts: 10064
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1131 :
No, why? As I said, ref is just a pointer. It can point to anything and its size would still be the same.
If you like our products and services, please help us by posting your review here.
-
- Site Admin
- Posts: 10064
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1131 :
As an exercise, try running this code:
Code: Select all
public class RecursiveObject {
public RecursiveObject obj = new RecursiveObject(); //Make it static and try again
int[] ia = new int[1000];
public static void main(String[] args) {
RecursiveObject ro = new RecursiveObject();
}
}
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Tue Oct 06, 2015 4:54 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1131 :
Thanks for the utterly useless explanation of "non sense!", really helped me out.
-
- Site Admin
- Posts: 10064
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1131 :
Not sure what explanation would one need here. The statement is simply not true. Anyway, it has been updated to make it more clear.
thank you for your feedback!
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 72 guests