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 6 of 6

Thread: please solve my problem with the constructors

  1. #1
    Junior Member Maicol's Avatar
    Join Date
    Jan 2021
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default please solve my problem with the constructors

    Hello! I'm benniger and I don't understand why Eclipse says that there are a error in the default constructor and that there is not a return type for the other constructor. thanks you
    package fuente;
     
    public class User {
    	public static void main(String[] args) {
    		final String account;
    		final String password;
     
    		public User(){	
    		}
     
    		public User(String account, String password) {
    			this.account=account;
    			this.password=password;
    		}
     
    		private void setAccount(String newAccount) {
    			account=newAccount;
    		}
     
    		private void setPassword(String newPassword) {
    			password=newPassword;
    		}
    	}
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: please solve my problem with the constructors

    Please copy and paste here the full text of the error messages that show what lines in the source file have the errors.

    One error I see is the ending } for the main method is misplaced. It should be before the declaration of the constructor.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Maicol (January 3rd, 2021)

  4. #3
    Junior Member Maicol's Avatar
    Join Date
    Jan 2021
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: please solve my problem with the constructors

    Thanks Norm. I corrected the first error, now this is my code:
    package fuente;
     
    public class User {
    	public static void main(String[] args) {
    		final String account;
    		final String password;
    	}
    		public User(){	
    		}
     
    		public User(String account, String password) {
    			this.account=account;
    			this.password=password;
    		}
     
    		private void setAccount(String newAccount) {
    			account=newAccount;
    		}
     
    		private void setPassword(String newPassword) {
    			password=newPassword;
    		}
     
    }
    In the line 12 says "accaount cannot be resolved or is not a field".
    In the lines 17 and 21 says "account cannot be resolved to a variable".
    Can ypu say me why the main method must be close before or constructors?
    thanks you again.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: please solve my problem with the constructors

    The fields need to be declared at the class level outside of any methods. Move the declarations out of the main method to the class level.

    why the main method must be close
    You can not declare methods or constructors inside of other methods.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Maicol (January 3rd, 2021)

  7. #5
    Junior Member Maicol's Avatar
    Join Date
    Jan 2021
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: please solve my problem with the constructors

    So what must to write in the main method?

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: please solve my problem with the constructors

    The main method needs to have the code to be executed to start the execution of the code in a class. That is up to the programmer to decide.

    When you give the command: java ClassName args... to the OS, it starts the java program which looks in the ClassName class for the main method and calls it passing it the value in args... in a String array.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    Maicol (January 5th, 2021)

Similar Threads

  1. Help Me to Solve this Problem..
    By hardik mehta in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 28th, 2014, 06:13 AM
  2. please solve the problem
    By liz in forum Threads
    Replies: 1
    Last Post: February 6th, 2014, 05:27 AM
  3. please solve my problem
    By joy1604 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 8th, 2013, 08:34 PM
  4. Replies: 7
    Last Post: April 5th, 2013, 10:18 PM
  5. [SOLVED] Overloading constructors(Multiple Constructors)
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 11th, 2011, 12:55 PM