wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes creating object out of class of same name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "creating object out of class of same name" Watch "creating object out of class of same name" New topic
Author

creating object out of class of same name

Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
I'm confused about instantiating a class from within a class of the same name. I know it's an array of objects, but if you are declaring class Mix4, can you then create more Mix4 classes from within Mix4?? What's in Mix4 that is being recreated 20 times? The only thing that I can see in there is a variable called counter = to 0. What am I missing?

The following is from Head First Java (a really great beginner's book, but even that one may be beyond me!!!)



Also, this line confuses me. Can you keep recreating the same object over and over???


The whole business of creating and instantiating classes in all these different ways boggles my mind.

Can anyone suggest a really good beginner's book? I've read the list on this website, but if anyone has actual experience with it, I'd like to know.

Thanks for any help. Von
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Vonique Leary:
... Can you keep recreating the same object over and over??? ...

Think of a class as a blueprint for building an object. When you say "new Mix4()," you are saying build me a new object based on the blueprint Mix4. You can use the same blueprint (class) to build as many new objects as you like.

The array m4a has a size of 20. When it's created, the 20 elements are initialized to null references. Not all of these are assigned instances of Mix4. If you add some println statements to the code, you can see where the new objects are actually being created...

So can you see which of the array elements are still null when this is done?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
Not all of these are assigned instances of Mix4.


Doesn't this:



mean they are all of type Mix4's?

I can see that some are being created inside main and some inside MaybeNew but I can't see which are null. In fact, the only thing I can see that Mix4 contains is a counter variable.

To me it looks like objects are being created inside the same class that defines it. In other places in the book it says that you need to classes to create an object. One to define it and another one with a main method to instantiate it. I think this is where I might be going wrong.

Am I way off track?
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
To me it looks like objects are being created inside the same class that defines it. In other places in the book it says that you need to classes to create an object. One to define it and another one with a main method to instantiate it. I think this is where I might be going wrong.


In the above sentence I meant to say In other places in the book it says that you need two classes to create an object.
[ April 15, 2008: Message edited by: Ulf Dittmer ]
Hari Srinivas
Ranch Hand

Joined: Mar 26, 2008
Posts: 46
Originally posted by Vonique Leary:
To me it looks like objects are being created inside the same class that defines it. In other places in the book it says that you need to classes to create an object. One to define it and another one with a main method to instantiate it. I think this is where I might be going wrong.

In the above sentence I meant to say In other places in the book it says that you need two classes to create an object.



Ok let me clarify this... The object of the Mix4 class is created in the main method of the class. Here there is only one top level public class ie mix4 with some other functions apart from main.Since the main method is static and is the first method called during execution, it doesn't have any object of the class it belongs and hence is free to create an object of the same class to call other non-static methods.

Remember, since main is static you won't need an object to invoke it but other non static methods do need an object to invoke them
[ April 15, 2008: Message edited by: Ulf Dittmer ]
Hari Srinivas
Ranch Hand

Joined: Mar 26, 2008
Posts: 46
Can anyone suggest a really good beginner's book? I've read the list on this website, but if anyone has actual experience with it, I'd like to know.

Thanks for any help. Von


Dear Friend, as far as I have seen the "Head First Java" book definitely stands out and is also a fun book to read. The only other book that I can recommend at this point of time would be "Thinking in Java" by Bruce Eckel.

If after all that you face any difficulties...do not worry. Just post questions in these forums and there will always be ranchers to help you out.

Good Luck!
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
So, in other words, the main class is really not part of the Mix4 class and is therefore able to instantiate objects of Mix4? Okay, that's starting to make more sense, then.

But the other thing that confuses me is: what is actually in Mix4? As far as I can see this is the only thing that comprises Mix4:



Is MaybeNew() part of main or part of Mix4?

Thanks, and I do love the Head First Java book. But I do wish there were more line by line explanations of their examples to back up the tutorials.
Anyway, thank goodness for this forum where I've gotten some great hints to help me understand concepts I can't get explanations anywhere else!

Von
Hari Srinivas
Ranch Hand

Joined: Mar 26, 2008
Posts: 46
Originally posted by Vonique Leary:
So, in other words, the main class is really not part of the Mix4 class and is therefore able to instantiate objects of Mix4? Okay, that's starting to make more sense, then.

But the other thing that confuses me is: what is actually in Mix4? As far as I can see this is the only thing that comprises Mix4:



Is MaybeNew() part of main or part of Mix4?



Hey hang on.... Let me clarify things properly There's actually only one class and that is the Mix4 class.

The Mix4 class comprises of the following:
1. An integer variable called counter
2. the method (public static void) main from which the execution starts
3. the method MaybeNew()


Like I said before the method main is where all execution starts and hence at runtime you need to call it from command line. It is for this purpose it is declared static i.e. it can be called without using an object to invoke it.

To use any other variable(like in this case int counter) or member function
like MaybeNew(), which is not static, you need an object of that class to be created first.

This is exactly what you are doing in the methods main and MaybeNew()
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
Okay, I think I'm getting it now. It's all about the main method that is the reason a new Mix4 class can be created right within the Mix4 class. I reread your first reply:

Since the main method is static and is the first method called during execution, it doesn't have any object of the class it belongs and hence is free to create an object of the same class to call other non-static methods.


Now it's really starting to make sense!

If there wasn't a main method, could Mix4 create more Mix4's, like say within a method right in the same class of Mix4?

I know--I'm driving myself crazy with this, but I do get the idea of the static main class now.

Thanks so much!
Von
Hari Srinivas
Ranch Hand

Joined: Mar 26, 2008
Posts: 46
Ok...the main class need not always create object of the class it resides in....It can also do some operations on static variables and static methods (remember these do not need any objects to be invoked). If your class doesn't have the main method, it's methods can be invoked by creating objects from methods of other classes (which may either reside in the same source file or in a different source file).
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

first, there is no 'static main class'. there is a static main METHOD.

any method that is 'static' can be called even if no objects of that type have ever been created. This is how many of the 'Math' methods work... you don't need to create a Math object to call some of the methods there.

main is the same way. that method can be called when there are no objects of that type, and it isn't really tied to any specific instance of that object if 1 or more ARE created.

if Mix4 didn't have a main method, SOMETHING ELSE would... which is fine. to call a method from the Mix4 class, you'd need one of two things:

1) a static method in the Mix4 class - and yes, that static method could create a Mix4 object.

2) a constructor of some kind. This code doesn't have a constructor, so Java kindly provides you one that takes no arguments (you will never see it). However, if you write a constructor of any kind, Java will no longer provide you with the default no-arg one.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Hari Srinivas
Ranch Hand

Joined: Mar 26, 2008
Posts: 46
hi Mr Fred...i hope you can help him out by giving this guy some links to java ranch articles on some basics...i have been trying to find them ....but haven't got something suitable yet

Thanks
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Vonique Leary:
... In other places in the book it says that you need [two] classes to create an object. One to define it and another one with a main method to instantiate it. I think this is where I might be going wrong...

Yes, that's where you're going wrong.

A main method is an entry point for a program. Basically, it's what gets called when the program starts. A program might use several different classes, and one of these classes needs a main method as that starting point. Normally, the main method calls at least one constructor to instantiate an object and get things going. But constructors can be called from other places too, including methods within the same class. (In fact, sometimes constructors are made private so they must be called from within the same class.)
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Vonique Leary:
... Doesn't this:

mean they are all of type Mix4's?

I can see that some are being created inside main and some inside MaybeNew but I can't see which are null...

new Mix4[20] creates an array of size 20 that can hold references to Mix4 objects.

But when you first create the array, there is nothing "in" it. The array holds 20 references, and the type of these references is Mix4, but none of these references point to any Mix4 objects yet, so they are only null references.

Now when you say m4a[x] = new Mix4() you are creating a new Mix4 object, and a reference to that object is assigned to a particular index (x) of the array.

Try adding some code to the end of the main method that loops through the array and prints out each element. This would be a good exercise that will show you exactly what's in the array.
S Reddy
Ranch Hand

Joined: May 17, 2007
Posts: 45
If there wasn't a main method, could Mix4 create more Mix4's, like say within a method right in the same class of Mix4?


You can create the object of same class from any method.
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
Well, I'm alot clearer on it by now. But one more thing bothers me:

marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Vonique Leary:
...Mix4 m4 = new Mix4(); // Doesn't this keep creating the
//same object over and over again?...

It creates an instance of the same class over and over. But each time "new" is used, a new object is created.

A reference to the new object is assigned to the variable "m4." It might seem like m4 keeps getting reassigned to point to new objects. But note that "m4" is a local variable (local to the method body). That variable is created when maybeNew is invoked, and destroyed when maybeNew exits. The new objects created inside this method are not accessible from outside the method, and nothing is ever "done" with them.
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
So, if m4 is destroyed after the local method runs, what happens to what's done with it inside the code:



I assume that since counter is an instance variable, it does not get destroyed after the method has run? Is that the reason m4 has to keep getting recreated, so that the counter variable can be incremented each time?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Vonique Leary:
So, if m4 is destroyed after the local method runs, what happens to what's done with it inside the code:



I assume that since counter is an instance variable, it does not get destroyed after the method has run? Is that the reason m4 has to keep getting recreated, so that the counter variable can be incremented each time?

That code is still inside the method, so the variable "m4" is still valid. After the method exits and the variable is destroyed, the object that it referenced becomes eligible for garbage collection because it's unreachable.

As you noted, "counter" is an instance variable, meaning that each instance of Mix4 has its own counter. When a new instance of Mix4 is created, its instance variable "counter" has an initial value of 0. That value is incremented to 1 immediately after the object is created. But no object's "counter" is ever incremented past 1.

On the other hand, "count" is a variable that's local to the main method. This variable is incremented whenever a new Mix4 object is created in main and whenever the method maybeNew returns a value of 1. The method maybeNew returns a value of 1 if it creates a new object (which happens only when x is less than 5).

Note the additional comments and println statements I added to the code. If you compile and run this, I think the output will show you what's happening.

[ April 18, 2008: Message edited by: marc weber ]
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
Thank you, the println's really helped in my understanding of what goes on with local variables and their lifetime in the Mix4 program.

Question: What's the real purpose of a local variable if it just gets destroyed after the method runs? And what if you wanted to use the value of that variable after the method runs, say in some calculation? How would you keep that value? Would you just make it an instance variable (like you did with the x variable?). Why can't they all be instance variables? Resources?

I know the answers to the above should be obvious but to a beginner like me, everything needs to be spelled out.

Thanks, you have been so helpful!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

You might think of local variables as "temp" variables. They only exist while they are needed to perform some process within the method. After that, they are just taking up resources. So yes, if you need to retain a value outside of a method, then it needs to be stored in a variable outside the method (e.g., an instance variable).
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

If I listen to a message on my answering machine and someone says "This is X, please call me at phone number Y about such and such" then I will grab a scrap of paper and write down Y so I can call X. Then I will throw away the scrap of paper after I call.

That's a local variable in the "answerRecordedMessage" method.

But if after I talk to X, I decide I need to keep her phone number so I can call her on a regular basis in the future to discuss my investments, I will put it into my phone directory so I can do that.

That's an instance variable in the "PhoneContact" class.
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
Thanks, that's a nice explanation. Great help I've gotten here!

Just one last question:

Are you guys really bartenders?

Thanks,
Vonique
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Saloon Titles.
Vonique Leary
Ranch Hand

Joined: Mar 24, 2008
Posts: 107
Wonderful! I hadn't seen that page.

Well, thanks for all the helpful replies. I've decided to take an online Java class and I'm sure I'll be back frequently. I'll need all the help I can get and this is such a great place to get it!

Vonique
Ed Zychowski
Greenhorn

Joined: Jan 16, 2010
Posts: 1
Thanks. I'm just learning and this one threw me too. So early in the book, I was really concerned if I could learn this or not. The commented code really put me back on track!


A thousand lemmings can't be wrong...?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: creating object out of class of same name
 
Similar Threads
return statement
code
return - problem
how exactly does this code compile?
How to read this?