aspose file tools*
The moose likes Beginning Java and the fly likes Class instantiation within the same class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class instantiation within the same class" Watch "Class instantiation within the same class" New topic
Author

Class instantiation within the same class

Tom Grove
Greenhorn

Joined: Dec 16, 2008
Posts: 1
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
Sheriff

Joined: Sep 28, 2004
Posts: 19066
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19066
    
  40

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

Joined: Nov 05, 2008
Posts: 50
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class instantiation within the same class