About Question enthuware.ocajp.i.v7.2.1131 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
ETS User

About Question enthuware.ocajp.i.v7.2.1131 :

Post by ETS User »

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

admin
Site Admin
Posts: 10064
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1131 :

Post by admin »

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
If you like our products and services, please help us by posting your review here.

Guest

Re: About Question enthuware.ocajp.i.v7.2.1131 :

Post by Guest »

So if you did
static TestClass ref = new TestClass();

would it be an error?

admin
Site Admin
Posts: 10064
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1131 :

Post by admin »

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.

admin
Site Admin
Posts: 10064
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1131 :

Post by admin »

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.

ncowan
Posts: 1
Joined: Tue Oct 06, 2015 4:54 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1131 :

Post by ncowan »

Thanks for the utterly useless explanation of "non sense!", really helped me out.

admin
Site Admin
Posts: 10064
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1131 :

Post by admin »

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!
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 72 guests