Results 1 to 6 of 6
- 01-09-2013, 05:51 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 5
- Rep Power
- 0
Please explain static void and void methods are ?
I know void means a that a value does not have a return in a method . but according to this site( Java - Methods) example it returns a value of C .Please explain this to me if I'm wrong. and please explain what is static void means with examples .
Below is the void method example from the site
public class TestVoidMethod {
public static void main(String[] args) {
printGrade(78.5);
}
public static void printGrade(double score) {
if (score >= 90.0) {
System.out.println('A');
}
else if (score >= 80.0) {
System.out.println('B');
}
else if (score >= 70.0) {
System.out.println('C');
}
else if (score >= 60.0) {
System.out.println('D');
}
else {
System.out.println('F');
}
}
}
C
- 01-09-2013, 06:41 PM #2
Senior Member
- Join Date
- Feb 2012
- Posts
- 219
- Rep Power
- 2
Re: Please explain static void and void methods are ?
It does not return anything, it just prints it out.
- 01-10-2013, 03:13 PM #3
Member
- Join Date
- Dec 2012
- Posts
- 42
- Rep Power
- 0
Re: Please explain static void and void methods are ?
a returned value is distinct because it will be a method that has the word return in it, then what happens is when that method is called is equals the return value, i hope im saying that right.
with the returnMethod() when you call this method in your main arg class you would see something like thisJava Code:public class ReturnExample { public int x = 10; public int returnMethod(){ return x; } }
so what will happen is you will have assigned the value of x in your ReturnExample class to z in your main class, then you print it out it will just print out the value of 10.Java Code:public static void main(String[] args){ private int z; ReturnExample test = new ReturnValue; z = test.returnMethod(); System.out.println(z); }
a static variable just means it is the same for every object you create of that class, so if you have a value you want associated with every object in a class you would call it static, you can change a static value but when you do it changes for all objects of that class. unlike a non-static variable that when you change of one object it effects only that object.
i hope that clears it up a bit for you, and the code was just free typed so it may not be 100% correct but was mostly for the sake of explanation.
Russ
- 01-10-2013, 03:17 PM #4
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: Please explain static void and void methods are ?
ReturnExample test = new ReturnValue;
->
ReturnExample test = new ReturnValue();
EDIT: private int z; will throw you an error probably as private is meaningless inside a method...Last edited by Sierra; 01-10-2013 at 03:43 PM.
I like likes!.gif)
- 01-10-2013, 04:02 PM #5
Member
- Join Date
- Dec 2012
- Posts
- 42
- Rep Power
- 0
Re: Please explain static void and void methods are ?
yeah sorry, im still pretty new myself and rely alot on the IDE to catch noob mistakes that i make, but i thought i could try and shed a little light on this. noticed i created the class object wrong also, should probably be:
ReturnExample test = new ReturnExample();
not value.
sorry if i caused the OP any more confusion on the topic.
Russ
- 01-10-2013, 08:46 PM #6
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Similar Threads
-
public static void main(String... args)???
By SnakeDoc in forum New To JavaReplies: 5Last Post: 01-10-2013, 08:30 AM -
static void sleep()
By j2me64 in forum Threads and SynchronizationReplies: 4Last Post: 09-15-2012, 10:40 PM -
What is the difference between Public Static Void and Public Void?
By whateverme in forum New To JavaReplies: 1Last Post: 12-04-2010, 05:41 PM -
The different of static void,protected,and void in methods?
By Winarto in forum New To JavaReplies: 5Last Post: 01-24-2008, 11:53 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks