This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class help

 
antony magnier
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 )
 
antony magnier
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the very useful help.
 
Gautam Sathe
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

------------------
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic