Results 1 to 2 of 2
  1. #1
    silvia is offline Member
    Join Date
    Jul 2007
    Posts
    35
    Rep Power
    0

    Default Error: invalid method declaration

    I just wrote my second program to allow you to set and retrieve a username, password, and age for users.

    Java Code:
    public class Hello {
    	public User() {
    	}
    	public User(String username, String password, Int age) {
    		usrname = username;
    		usrpass = password;
    		usrage = age;
    	}
    	public void setUserName(String newusrname) {
    		usrname = newusrname;
    	}
    	public void setPassword(String newpass) {
    		usrpass = newpass;
    	}
    	public void setAge(Int newage) {
    		usrage = newage;
    	}
    	public String getUserName() {
    		return usrname;
    	}
    	public String getPassword() {
    		return usrpass;
    	}
    	public Int getAge() {
    		return usrage;
    	}
    	public static void main(String args[]) {
    		usr1 = new User();
    		usr1.setUserName("UserOne");
    		usr1.setPassword("PasswordOne");
    		usr1.setAge(1);
    
    		usr2 = new User("UserTwo", "PasswordTwo", 2);
    
    		System.out.println("User 1: " + usr1.getUserName + " " + usr1.getPassword + " " +usr1.getAge);
    		System.out.println("\n");
    		System.out.println("User 2: " + usr2.getUserName + " " + usr2.getPassword + " " +usr2.getAge);
    	}
    }
    ...And here's the errors I get when I compile it...
    Code:

    Java Code:
    C:\j2sdk1.4.2_11\bin>javac Hello.java
    
    Hello.java:2: invalid method declaration; return type required
            public User() {
                   ^
    Hello.java:4: invalid method declaration; return type required
            public User(String username, String password, Int age) {
                   ^
    2 errors
    ...I'm pretty sure I understand what these errors mean (return type would be like public String getPassword(), right?), but I just want these ... definitions (I dont know the word yet, new to the vocabulary) of User() to just be used for constructors...I don't want them to return anything.

    Thanks

  2. #2
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    The term "definition" u refer to, maybe u mean method?
    If ur makin a constructor, u gotta name it with the same name as ur class, ur class is Hello, so either u change the method name User into Hello or change the class name Hello into user

Similar Threads

  1. Replies: 3
    Last Post: 06-05-2010, 08:05 PM
  2. Replies: 3
    Last Post: 08-14-2009, 12:57 PM
  3. Declaration
    By asifahmed in forum New To Java
    Replies: 1
    Last Post: 04-05-2008, 05:38 AM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 2
    Last Post: 07-30-2007, 08:55 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
  •