Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: return type

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default return type

    public class Name {
    static int a;
    int b=0;
    public void fuck(int d){
    a=a+1;
    b=b+1;
    System.out.println(a);
    System.out.println(b);
    System.out.println(d);
    return ;

    the function is void type.. and in the end i also wrote return..but no value...
    the function runs with no error... why return is not creating a problem


  2. #2
    Member
    Join Date
    Jan 2013
    Posts
    39
    My Mood
    Relaxed
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: return type

    because you exit that "fuck" methode and it continues in the main methode. Return exits out of a methode and break breaks out of loops and if statments

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Location
    Earth
    Posts
    21
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: return type

    void int function_name()
    {
    ///code
    return variable_int;
    }

  4. #4
    Member
    Join Date
    Jan 2013
    Posts
    39
    My Mood
    Relaxed
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: return type

    You cant use "void int function_name(){}" because you have two return types and every methode only can have one.

    Here some examples(these are from a entitybase class but the prisiple is the same)
    private double x, y;
    	private double height, width;
    	private double velX, velY;
    	private boolean alive;
     
    	//accessor
    	public boolean getAlive(){return alive;}
    	public double getX(){ return x;}
    	public double getY(){ return y;}
    	public double getHeight(){return height;}
    	public double getWidth(){return width;}
    	public double getVelX(){return velX;}
    	public double getVelY(){return velY;}
     
    	//mutators
    	public void setAlive(boolean alive){this.alive = alive;}
    	public void setX(double x){this.x = x;}
    	public void setY(double y){this.y = y;}
    	public void incX(double i){this.x += i;}
    	public void incY(double d){this.y += d;}
    	public void setHeight(double height){this.height = height;}
    	public void setWidth(double width){this.width = width;}
    	public void incHeight(double i){this.height += i;}
    	public void incWidth(double i){this.width += i;}
    	public void setVelX(double velX){this.velX = velX;}
    	public void setVelY(double velY){this.velY = velY;}

    The accesor methodes will when used return a value(possible to be calculated within this methode it self). While the mutator methodes can change valeu and never have to return something because of their void return type. There is always 1 return type in every methode(void,int,double,float,string etc).

    And use the code tags for nice code layout.

  5. #5
    Member
    Join Date
    May 2011
    Location
    west palm beach, FL
    Posts
    189
    My Mood
    Tired
    Thanks
    41
    Thanked 11 Times in 10 Posts

    Default Re: return type

    you can have a return in a void method you just cant have it return anything

    return; works

    return anythingHere; shouldnt

Similar Threads

  1. covariant return type
    By debayanbh in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 14th, 2011, 09:27 AM
  2. abstract class & 'covariant' return type?
    By jezza10181 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 12th, 2011, 02:26 PM
  3. Ending a method that has no return type
    By Blehs in forum Java Theory & Questions
    Replies: 3
    Last Post: August 12th, 2011, 01:56 AM
  4. error: This method must return a result of type int
    By J05HYYY in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 13th, 2011, 05:26 PM
  5. Error of missing return statement while implementing Array
    By MS_Dark in forum Collections and Generics
    Replies: 1
    Last Post: December 10th, 2008, 03:18 PM