This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class instantiation within the same class

 
Tom Grove
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, this may or may not have been answered here but I'm not sure how to search for this on google. I've tried different variations but nothing seems to return the correct search results.

I am trying to figure why some example code shows a class and within that class shows an object being created with a class type of that class (make sense?). Here is an example:



The portion that I am concerned with is this:

QuizCardBuilder builder = new QuizCardBuilder();

Now, there are plenty of examples that I have seen that do not create an instance of the class within the class. To be honest, I would think that you would run into a never ending loop of objects being created over and over again.

So, can anyone explain to me when this is appropriate and why it is appropriate? Thanks.

Tom

[ December 16, 2008: Message edited by: Tom ]
[ December 16, 2008: Message edited by: Tom ]
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, there are plenty of examples that I have seen that do not create an instance of the class within the class. To be honest, I would think that you would run into a never ending loop of objects being created over and over again.


Just because the structure is complex, doesn't mean that it is an "endless loop of objects". But yes, it is possible to have endless construction, in which case you will run out of memory -- either out of memory error on the heap, or out of memory error with a stack overflow.


So, can anyone explain to me when this is appropriate and why it is appropriate? Thanks.


Well, complex data structures are generally used by complex programs. It's kinda hard to keep everything in one class when the code base gets really big. Nor is it a good idea to do so.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21405
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The portion that I am concerned with is this:

QuizCardBuilder builder = new QuizCardBuilder();

Now, there are plenty of examples that I have seen that do not create an instance of the class within the class. To be honest, I would think that you would run into a never ending loop of objects being created over and over again.


In rereading your question, I am thinking that you mean something else. Are you implying that the instantiation in main() will cause the endless loop?

First, of all, main() is a method (static in this case, but that's not important). In order for the instantiation to happen, someone has to call the method. In this case, it is by the JVM.

The constructor does not call main() -- creating a new instance doesn't call the main() method. So... only one instance is created.

Henry
 
Chadd Franck
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I can tell, Java requires a type for every variable that is declared

ie, String someString = new String();

String is the TYPE of variable;

someString is the variable TYPE String;

= sets the variable and it's type to refrence what it's pointing to.
In this case new String();

When you create a class ClassDog, you create it like this;

ClassDog newDog = new ClassDog();

ClassDog is the TYPE; newDog is the reference variable

Then when you create a instance variable with the same name, you don't declare it as the same TYPE but as as different TYPE ie;

String newDog = new String();

String is the Type; newDog is the reference variable.

with your example code

By declaring type QuizCardBuilder the JVM allocates memory for the "blueprint" of the class QuizCardBuilder, then, it assigns the refrence variable builder to refer to this specific class model, then with the new invocation actually builds the object from the "blueprint" .

The reason that you have to build the object in the main is that the JVM needs a way to tell what object to build first in your program. The JVM then accesses the class blueprint and runs the static method main, you can run a static method in a class before it becomes an object from just the blueprint of the object. The JVM runs the static method which then creates an object of the QuizCardBuilder class blueprint.

This gives life to


and under additional code there must be a non-static Go() method in this class because the line after the creation of the QuizCardBuilder class says

builder.go();

So if we added that to your code



While you can access static methods as well as static variables before an Object of the Class from the blueprint in this case the main method,

you cannot access any of the non-static methods until the class blueprint has been made into an Object with the new operator

Hope that makes sense;

Have a good day
[ December 16, 2008: Message edited by: Chadd Franck ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic