Results 1 to 9 of 9
  1. #1
    mcfer is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default invalid method declaration

    hello everyone,

    I am newbie at java programming. I am trying to run this code,


    class Puppy{

    int puppyAge;

    public Puppy(String name){
    // This constructor has one parameter, name.
    System.out.println("Passed Name is :" + name );
    }
    public setAge( int age ){
    puppyAge = age;

    }

    public getAge( ){
    System.out.println("Puppy's age is :" + puppyAge );
    return puppyAge;
    }
    public static void main(String []args){
    /* Object creation */
    Puppy myPuppy = new Puppy( "tommy" );

    /* Call class method to set puppy's age */
    myPuppy.setAge( 2 );

    /* Call another class method to get puppy's age */
    myPuppy.getAge( );

    /* You can access instance variable as follows as well */
    System.out.println("Variable Value :" + myPuppy.puppyAge );

    }
    }


    but i got the following errors.

    invalid method declaration; return type required
    public setAge(int age){

    invalid method declaration; return type required
    public getAge(){


    could anyone help me out with this ?
    thank you.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    All methods must have a return type. The "method" with the same name as the class is not a method, instead it's a constructor and has no return type.

    Generally setter methods will have a void return type indicating they they are doing something. Getters will have a type dependant on the variables being returneds type.

    Java Code:
    public void setSomething(int x){}
    public String getAString(){}

  3. #3
    mcfer is offline Member
    Join Date
    Jul 2011
    Posts
    7
    Rep Power
    0

    Default

    i understand method having same name is constructor.. but i suppose i did not declare the same name for method as class...so still i don't understand what is wrong ?

    Thank you though.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The constructor does not need a return type and has a name that is the same as a class.

    Declaring a method looks like this
    Java Code:
    <modifier> <return type> <name>(<args>){<body>}
    So a method can look like the things I showed above.

    Java Code:
    public String getString(){}
    public String toString(){}
    public int square(int x){ return x*x; }
    A constructor is different in that it doesn't have a return type. As the error suggests, your methods are set up similar to constructors(ie: no return type). The error says "Return type required", because you have methods which don'y have a return type.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Java Code:
    public getAge( ){
        System.out.println("Puppy's age is :" + puppyAge ); 
        return puppyAge;
    }
    Nitpick

    Getter methods generally do not print anything. The whole point of the method is to return the value back to the caller and it is the caller who will do something with that value such as print it out.

  6. #6
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    167
    Rep Power
    5

    Default

    You may also want to read up on method declarations and constructors. It is true that your method do not need to return something, but for that you need a modifier. Look that up.

    and you can have multiple constructors, but they must have a different signatures. So while they have the same name, the stuff inside the parentheses must be different.

  7. #7
    aavil32 is offline Member
    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Default Re: invalid method declaration

    Im new to java and i ran into the same problem just want to thank you guys for the help.

  8. #8
    Murali100 is offline Member
    Join Date
    Sep 2012
    Location
    Hyderabad
    Posts
    2
    Rep Power
    0

    Default Re: invalid method declaration

    This is the exact code
    class Puppy {
    int puppyAge;
    public Puppy(String name) {
    System.out.println("Passed Name is :" + name );
    }
    public void setAge( int age ){
    puppyAge = age;
    }
    public int getAge() //
    {

    System.out.println("Puppy's age is :" + puppyAge );
    return puppyAge;
    }
    public static void main(String []args){
    Puppy myPuppy = new Puppy( "tommy" );
    myPuppy.setAge( 2 );
    myPuppy.getAge( );
    System.out.println("Variable Value :" + myPuppy.puppyAge );

    }
    }

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: invalid method declaration

    Quote Originally Posted by Murali100 View Post
    This is the exact code
    You can't do much harm in a thread that's more than a year old, but please don't do this again. This is a forum, not a code factory -- don't deny questioners the learning experience that they come here for, just to prove your koding skillz.

    Also, that's bad code. Didn't you even bother to read the thread, including Junky's nitpick at #5? The lack of code tags and recommended vertical whitespace makes it difficult to read, which is just as well. The less people that read it, the better.

    Before you post again here, please go through these:
    Forum Rules
    Guide For New Members
    BB Code List - Java Programming Forum
    Code Conventions for the Java Programming Language: Contents


    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Invalid Method Declaration; Return Type Required
    By bremzb in forum AWT / Swing
    Replies: 3
    Last Post: 05-05-2011, 11:12 PM
  2. Err: invalid method declaration; return type required
    By Die The Villain in forum New To Java
    Replies: 12
    Last Post: 04-15-2011, 01:44 AM
  3. invalid method declaration error
    By bsarules in forum Java Applets
    Replies: 7
    Last Post: 05-25-2010, 07:06 PM
  4. BubbleSorting Problem: invalid method declaration
    By SwEeTAcTioN in forum New To Java
    Replies: 6
    Last Post: 12-02-2009, 04:47 AM
  5. Error: invalid method declaration
    By silvia in forum New To Java
    Replies: 1
    Last Post: 07-27-2007, 01:10 PM

Posting Permissions

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