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

Thread: Basic Question Regarding Object/Method

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Basic Question Regarding Object/Method

    So I have created a new class called employee and define a few employee'sL

    //Empty 
    Employee e1 = new Employee();
           //Good Values
           Employee e2 = new Employee("1234", "Bruce", "Wayne", "managment","Male", 1000000);
           //Bad Values
           Employee e3 = new Employee("10009", "abDcd", "dEf", "batman", "gender", -1);

    So now lets say I want to redefine whats in e3, would I just do something like:

    Employee e3 = Employee("1234", "cLiNT", "EASTWOOD", "management", "male", 100.5);

    Or would I have to create a method that somehow changes the next method.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Basic Question Regarding Object/Method

    The line "Employee e3 = Employee("1234", "cLiNT", "EASTWOOD", "management", "male", 100.5);" will, almost certainly, not compile. (Try it.)

    void foo() {
            // (A)
        Employee e3 = new Employee("10009", "abDcd", "dEf", "batman", "gender", -1);
            // (B)
        e3 = new Employee("1234", "cLiNT", "EASTWOOD", "management", "male", 100.5);
    }
     
    void bar() {
            // (C)
        Employee e3 = new Employee("1234", "cLiNT", "EASTWOOD", "management", "male", 100.5);
            // (D)
        e3.setId(666);
    }

    There are four different things going on here:

    (A) Declares a variable of type Employee, creates a new employee and assigns a reference to this new employee to the variable
    (B) Creates another employee (a different person) and changes the value of the variable so that it becomes a reference to this second employee.
    (C) Declares a variable of type Employee. The variable is e3 but it has nothing to do with the e3 variable in (A) and (B). I have shown it in another method, but it could equally well be in foo(), but in a different scope (in a for loop for example). As in (A) a new employee is created and a reference to this new person is assigned to the variable.
    (D) Assuming setId() is defined somewhere, this line alters the ID of the third person.

    (A) and (C) declare (==introduce) new variables to "stand for" people. (B) and (D) do not.

    (A), (B) and (C) create new people. (hence the "new" keyword). (D) does not.

    (B) alters the value of an existing variable. While (D) alters the state of an existing person. I am guessing that this is the distinction you are mostly concerned about. English is notoriously ambiguous and both could be described as "changing what's in e3".

    You don't "have to" do one of these rather than another. You do whatever is appropriate: sometimes you want to change the values of variables (the people they refer to), sometimes you want to change some aspect of the people themselves.
    Last edited by pbrockway2; February 26th, 2012 at 01:22 PM.

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

    aandcmedia (February 26th, 2012)

  4. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Basic Question Regarding Object/Method

    Thanks appreciate the help!

  5. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Basic Question Regarding Object/Method

    You're welcome.

Similar Threads

  1. basic java question
    By jim213mm in forum Java Theory & Questions
    Replies: 2
    Last Post: January 19th, 2012, 01:30 PM
  2. Question about object passing to a method? My VOCAB sucks
    By tripline in forum Object Oriented Programming
    Replies: 6
    Last Post: October 28th, 2011, 12:48 PM
  3. Matcher object .find() method question
    By chronoz13 in forum Java SE APIs
    Replies: 18
    Last Post: September 8th, 2011, 11:18 AM
  4. Very new to Java basic question
    By loofy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 23rd, 2011, 06:21 PM
  5. [SOLVED] Asking what I suspect to be a very basic question
    By Noobert in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 24th, 2010, 07:42 AM