Results 1 to 6 of 6
Like Tree2Likes
  • 1 Post By Wnt2bsleepin
  • 1 Post By Sierra

Thread: Please explain static void and void methods are ?

  1. #1
    shavindra is offline Member
    Join Date
    Dec 2012
    Posts
    5
    Rep Power
    0

    Question 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

  2. #2
    Wnt2bsleepin is offline Senior Member
    Join Date
    Feb 2012
    Posts
    219
    Rep Power
    3

    Default Re: Please explain static void and void methods are ?

    It does not return anything, it just prints it out.
    Sierra likes this.

  3. #3
    Russd772 is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default 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.

    Java Code:
    public class ReturnExample {
    
    public int x = 10;
    public int returnMethod(){
    return x;
    }
       }
    with the returnMethod() when you call this method in your main arg class you would see something like this

    Java Code:
    public static void main(String[] args){
    
    private int z;
    
    ReturnExample test = new ReturnValue;
    
    z = test.returnMethod();
    System.out.println(z);
    
    }
    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.

    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

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default 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 04:43 PM.
    DarrylBurke likes this.
    I like likes!

  5. #5
    Russd772 is offline Member
    Join Date
    Dec 2012
    Posts
    42
    Rep Power
    0

    Default 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

  6. #6
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Please explain static void and void methods are ?

    Are you doing a K12 class? I remember doing something similar to this.

Similar Threads

  1. public static void main(String... args)???
    By SnakeDoc in forum New To Java
    Replies: 5
    Last Post: 01-10-2013, 09:30 AM
  2. static void sleep()
    By j2me64 in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-15-2012, 11:40 PM
  3. Replies: 1
    Last Post: 12-04-2010, 06:41 PM
  4. Replies: 5
    Last Post: 01-25-2008, 12:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •