• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

set Vs get?? static vs. void??

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code below is working, but I don't understand why in the "public String getTopping", we use "String", and we use "void" in the setTopping".? What's the differences between the void and static.
public class Pizza
{
private String strTopping;
//get the topping
public String getTopping()
{
return strTopping;
}
//assign the value to paramter
public void setTopping(String topping)
{
strTopping = topping;
}
}
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Mindy Wu:
[B]The code below is working, but I don't understand why in the "public String getTopping", we use "String", and we use "void" in the setTopping".? What's the differences between the void and static.
[/B]


The thing before getTopping or setTopping is the return type. When you are getting the topping in getTopping, you want to return it as a String, so you say

When you are setting the topping, you don't want a return value, so you say

"void" indicates that nothing is returned to the caller.
- Susan
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mindy,
as Susan says, 'String' and 'void' in this context are return types. You use that when declareing your methods to indicate whether your method should return something as a result, and in case, what kind of result. (what datatype kind)
If you want your method to return something (as in your 'getTopping()' method), you must specify what datatype the method that called your method should expect to get as a result.
That can be a String, an int, a HashTable, a MindyWuMightyCoolObject or whatever. If you don't need a result from your method, you should use 'void' to indicate that it should not return anything.
'Static' does not apply to datatypes. You use 'static' to indicate that the method (or variable) should be used independent of any actual objects that are created. This means that you don't have to make an instance of the object to use the method or variable. This can be useful in many situations. You have probably seen the 'static' keyword with the 'main' method, which always looks like this: 'public static void main(String[] args)'. The 'main' method is a very special method, and not very suitable for examples, but the above is still true about the main method; when your class is instantiated, it is done through the 'main' method, which in many respects should be viewed as outside the class instance. Better examples are for instance methods in the 'java.lang.Math' package, where you will find many methods and variables declared static. Math computations are generally CPU-intensive operations, and therefore some commonly used literals like PI (ca. 3.1415926) are declared and ready to use. This means you don't have to instantiate (in fact you cannot) a 'Math' object to access these values. Furthermore, they will be stored only once, not once for every object that uses these values. The same applies to methods like 'sqrt()' (square root) which in common math is always calculated in the same way, letting this method take up space only once.
Note that a method can only return one (or zero) values, which is why you will only see methods declared with one return type.
(Never for instance: public String String int myMethod() )
Hope that helps!
Marius
 
Mindy Wu
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks folks,this really helps me.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic