wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes class help 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 "class help" Watch "class help" New topic
Author

class help

antony magnier
Greenhorn

Joined: Jul 10, 2001
Posts: 11
need some help about classes in java. Any help would be appreciated. the following is an extract from a java book.
public final int heads=0;
public final int tails=1;
private int face;
public coin ()
{
flip();
}
public void flip()
{
face= (int) (math.random()*2);
}
public int getface()
{
return face;
}
Now for the questions:
what does flip() do in the public coin part?
why is void used in public void flip? what does void do? why isnt it used for public coin? why cant int be used in public void flip like in the one before it?
sorry about the number of questions, any help would be very welcome. Thank you.
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
Originally posted by antony magnier:
need some help about classes in java. Any help would be appreciated. the following is an extract from a java book.
public final int heads=0;
public final int tails=1;
private int face;
public coin ()
{
flip();
}
public void flip()
{
face= (int) (math.random()*2);
}
public int getface()
{
return face;
}
Now for the questions:
what does flip() do in the public coin part?
why is void used in public void flip? what does void do? why isnt it used for public coin? why cant int be used in public void flip like in the one before it?
sorry about the number of questions, any help would be very welcome. Thank you.


Okay... there are a few things here you must understand. But I'll answer your questions right out
Answer to 1)
what does flip() do in the public coin part?
flip() is a method call. It is calling the method below called flip.
Answer to 2)
why is void used in public void flip?
void is what is going to be returned. VOID means nothing will be returned. So whoever is calling it can expect nothing to be returned.
Answer to 3)
what does void do?
void is a keyword telling the method that it will return nothing
Answer to 4)
why isn't it used for public coin?
I'm not sure why it isn't used. If coin is a method, it should have a return type marked. void or some other type should be there to show.
why cant int be used in public void flip like in the one before it?
It can be, but as you can see, the value that is being modified is in the class (or what is called a member variable) and doesn't need to be passed back because it is already modified.
Hope this helps.
-Dale

------------------
What's this H2SO4 doing in my fridge?? ( thud )


By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
antony magnier
Greenhorn

Joined: Jul 10, 2001
Posts: 11
Thanks for the very useful help.
Gautam Sathe
Greenhorn

Joined: May 24, 2001
Posts: 18
I think public coin() is a constructor with no argument, that's why it is not returning any value.
Constructors are special type of methods which doesn't have a return type and are implicitly invoked during object instantiation.

------------------
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class help
 
Similar Threads
"javax.crypto.BadPaddingException: pad block corrupted" using BouncyCastle and DESede: How to avoid?
enums and switches
using enum with JDK1.5
Framework for GUI with computationally intensive threads
producing javadoc