0

can someone explain what variables do what in beginning Java? I am confused about what the variables point to in the bit of code I am doing for my computer class. I am too new to even know what to search for to get the right answer and the book I am using doesn't help explain anything as it names all the variables the same which confuses me on what is called from where. Thank you Chad

Where does the public void setEmpID(int NewID) set this.empID to NewID?

// access modifier is set to public and a class is declared and named Employee
public class Employee  
{   
    private int empID; 
    private String firstName; 
    private String lastName; 
    private double monthlySalary; (Are these to store the variable values from the main method until class creation or are they defining variables in the object that will be created?)

    //Constructor intializes class Employee to create object Employee with instance variables above. Must be same name as class

    **public Employee(int empID, String firstName, String lastName, double newSalary) //(is this the storage until the class is created or the defining of the variables for the object that will be created?)**
    {
        //ensures empID is a positive number before assignment
        if (empID > 0.0) 
            //assigns empID to variable "empID"
            **this.empID = empID; //where does this assign empID? in the new object or in this class temporarily until the object is created?**

        //assigns firstName to variable "firstName"
        this.firstName = firstName; 

        // assigns lastName to variable "lastName"
        this.lastName = lastName;                         

        //ensure monthlySalary is a positive number before assignmentand if 
        //ends constructor
    }

    **//method that sets the empID(where are these set?)**
    public void setEmpID(int newID)
    {
        this.empID = newID;
    }

    //method that sets the firstName
    public void setFirstName(String newFirst)
    {
        this.firstName = newFirst;
    }

     //method that sets the lastName
    public void setLastName(String newLast)
    {
        this.lastName = newLast;
    }

    //method that sets the monthlySalary for the new obj
    public void setMonthlySalary(double newSalary)

    {
        this.monthlySalary = newSalary;
    }

    **//Gets empid from the object and returns empID to the calling method (I think this is right)**
    public int getEmpID()
    {
        return empID;
    }  

    //gets first name from the object and returns first name to the calling method  
    public String getFirstName()
    {
        return firstName;
    }  

    //gets last name from the object and returns last name to the calling method  
    public String getLastName()
    {
        return lastName;
    }  

    //gets monthly salary from the object and returns it to the calling method  
    public double getMonthlySalary()
    {
        return monthlySalary;          
    }

}
  • Just so you know the `**` are not required for your comments – Dan Feb 02 '17 at 20:39
  • ok thanks. I wanted to bold the parts I had questions about just to make it easier to see the parts so people reading the post could quickly find the code in question. For some reason when I ** the parts they weren't boldface when posted. – chad jensen Feb 03 '17 at 15:12
  • Oh, that was because they were indented so the question does not recognise it as bold – Dan Feb 03 '17 at 15:13
  • Also did my answer help clear things up? – Dan Feb 03 '17 at 15:16
  • I just figured out why. The brackets were seen by the forum software and it just posted verbatim everything in between. – chad jensen Feb 03 '17 at 15:19

1 Answers1

1

If you want to know about variable visibility this might help. SO Question.

As for when variables have the same name see the code below

Example.java

public class Example {
    private int empID = 0; //Creates a global variable

    public Example(int empID) { //Creates a local variable that is 
                                //initiated when contructor is called
        this.empID = empID; //Calls the global variable and sets it
                            //equal to the local variable
        exampleMethod(); //Calls the method below
    }

    private void exampleMethod() {
        empID = 1; //Sets the global variable to = 1
        //This is the same as this.empID = 1;
    }

    public void setEmpID(int newID) {
        empID = newID; //Sets the global variable to = newID
        //This is the same as this.empID = newID;
    }

    public int getEmpID() {
        return empID; //Gets the value of the global variable
                      //empID. Example use below
    }
}

Example2.java

public class Example2 {
    public Example2() {
        Example ex = new Example(1); //Create and initate an Example class
        System.out.println(ex.getEmpID()); //Prints out Example.empID, aka 1
        ex.setEmpID(2);
        System.out.println(ex.getEmpID()); //Prints out Example.empID, aka 2
    }

    public static void main(String[] args) {
        new Example2(); //Creates and initates an Example2 class
    }
}

And as for variable types this short tutorial goes through them. Tutorial

Community
  • 1
  • 1
Dan
  • 7,286
  • 6
  • 49
  • 114
  • So global variables are what are in the (in this program's case) main method? Which I defined as employee employeeTwo = new employee(100, John, Doe, 3000.01) I know that the first employee is telling to call the "employee" class and the second is telling the main method to create a object from that class named employeeOne and it will be a new employee with the (attributes) are the attributes in parenthesis the global variables? Thank you guys for the wicked fast response!! You guys rock! – chad jensen Feb 02 '17 at 20:25
  • I'm not entirely sure what you mean by your first questions. Global variables can be seen by an entire class, whereas local variables can only be seen by a single method or constructor. – Dan Feb 02 '17 at 20:34
  • @chadjensen As for your second part. `Employee employeeTwo` creates a variable. and `employeeTwo = new Employee(100, John, Doe, 3000.01);` initiates the variable so that it can be used. For example in my code example if you ran `Example ex;` and then `System.out.println(ex.getEmpID());` you would get an error – Dan Feb 02 '17 at 20:37
  • @chadjensen Does this help clear everything up? – Dan Feb 02 '17 at 20:40
  • Dan, your examples are amazing. thank you for taking the time to explain to me. It helped out so much. – chad jensen Feb 03 '17 at 15:16
  • @chadjensen No problem. Would you mind clicking the tick next to my answer if you think it is sufficiently correct? – Dan Feb 03 '17 at 15:17
  • On your example why would you print out empID from your Example2 class before you initiate your main method, which creates your Example2 class. – chad jensen Feb 03 '17 at 15:34
  • Or are you just doing like I did, except instead of using two screens to keep them separate(easier to visualize them as being different for a noob) you put them on the same screen? – chad jensen Feb 03 '17 at 15:37
  • I don't. When the main method initiates the `Example2` class it runs the code inside the constructor and I have two examples to show how methods from `Example` can be used in `Example2`. Other than that I'm not entirely sure what you mean – Dan Feb 03 '17 at 16:58