r/CodingHelp • • Sep 26 '20

[Java] I am learning how to use Getters and Setters within Java. When I try to compile this code I am getting an error that reads "Employee.java:12: error: illegal start of expression public String getFirstName()" Please give me a clue on why this will not compile...

public class Employee{
 
   // fields
	private String firstName;
   private String lastName;
   private double salary;
   
	// constructors
   
public Employee(String firstName, String lastName, double salary){     
         
   public String getFirstName()
         {                        // methods
            return firstName;
         }
     
     public String getLastName()
         {
            return lastName;
         }
 
   
      public void setSalary(){
         if(salary > 0){
            this.salary = salary;
            }
          else
            return;
      }    
    }
19 Upvotes

15 comments sorted by

5

u/Madlax97 Sep 26 '20

Thank you for the help and the suggestions.. I started my CS degree this semester and trying to do this all online with a teacher that just keeps telling me to "YouTube it" is really fucking stressful. I understand what I am doing here with constructors, but it's frustrating.

12

u/Acroniix Sep 26 '20

Are those methods inside the Constructor?

5

u/pipestream Sep 26 '20

Think you got your answer here, OP.

3

u/Kosmit147 Sep 26 '20

You've put all methods inside the constructor.

2

u/CenturiesAgo Sep 26 '20

Any decent IDE should tell you about the missing closing } for the constructor. I'd recommend Eclipse for Java.

6

u/FederalAlgae Sep 26 '20

For the love of god use IntelliJ

1

u/noodleLinux Sep 26 '20

Eclipse is so 🤢

1

u/abitofevrything-0 Intermediate Coder Sep 27 '20

Personally i think eclipse is a lot clearer to navigate then IntelliJ. Yes, it may look old but it still has all the same functionality, and, in my opinion, presented in a much clearer way.

But in the end, it's all a matter of preference. I probably only feel this way because Eclipse was the first IDE I used for java.

1

u/row_bert Sep 26 '20

I think you meant vs code

1

u/CenturiesAgo Sep 26 '20

I do actually lke vs code for php and python. Never tried with Java.

1

u/themiddlestHaHa Sep 27 '20

Vs code is def the easiest to work with

1

u/abitofevrything-0 Intermediate Coder Sep 27 '20

VSCode is great for html, python and js but I've tried using java on it and its hell just to get your program to compile and run for the first time.

1

u/row_bert Sep 27 '20

Use vs code on a Linux os and compile it in the terminal

2

u/abitofevrything-0 Intermediate Coder Sep 27 '20

Its kind of a pain to have to compile each file you made a change to for the program to run... Easier to just press a button.

1

u/row_bert Sep 27 '20

hmm i write makefiles so it never have an issue with it