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: Objects

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Objects

    i just want to ask this for some clarification

     
    myObjectOne = myObjectTwo.getTheValue(); // assuming this method returns an object(reference(

    in the sample code.. what is myObjectOne? is it the receiving object? or is it the calling object that refers to the returned object value?

    what is myObjectTwo? is it the receiving object? or is it the calling object that calls the method?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Objects

    myObjectOne is the receiving object. It's "receiving" the object reference from the method getTheValue(). myObjectTwo is the calling object, and any data associated with it can be used by the getTheValue() method.

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

    chronoz13 (January 20th, 2010)

  4. #3
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Objects

    something is confusing here.. the book says the reserved word "this" is used to refer to the receiving object

    for example
    myObjectOne.doThisMethod();


    inside the doThisMethod()
    // assuming that these are the codes inside the doThisMethod();
     
    public voi doThisMethod() {
     
      this.getTheFirstValue();
      this.getTheSecondValue();
    }

    in this case the reserved word this is reffering to the myObjectOne object...

    does the book teaching me somethin confusing?

    by the way.. this is my exact assumption on what the book have said....

    that the reserved word "this" is used to refer to the receiving object

  5. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Objects

    No you use the keyword this inside your class to represent the instance of the class itself. For instance take a simple getter method.

    public class Test {
     
        private String string;
     
        public String getString() {
            return [b]this[/b].string;
        }
    }

    Here you can see that the keyword this is referring to this instance of the class and it then returns the member called string from it.

    // Json

  6. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Objects

    oh i forgot to include this.. assuming that the myObjectOne is an instance of its class...

    darn... the book is a bit confusing...

  7. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Objects

    Even if you have multiple instances of an object, this will always refer to the current object the method is being called from.

    public class Demo
    {
         String toDisplay;
         public Demo(String toDisplay)
         {
              this.toDisplay = toDisplay;
         }
     
         public void display(Demo demo2)
         {
              System.out.println("I'm displaying this demo's toDisplay: " + this.toDisplay);
              System.out.println("Now displaying demo2's toDisplay: " + demo2.toDisplay);
         }
     
         public static void main(String[] args)
         {
              Demo d1 = new Demo("demo1");
              Demo d2 = new Demo("demo2");
              d1.display();
          }
    }

Similar Threads

  1. [SOLVED] Word Scramble Using Objects & Methods
    By Whitechapel in forum Object Oriented Programming
    Replies: 3
    Last Post: May 23rd, 2010, 12:38 PM
  2. Arraylist Objects to Database
    By frankycool in forum JDBC & Databases
    Replies: 3
    Last Post: November 15th, 2009, 08:01 PM
  3. Arrays Of Objects?
    By MysticDeath in forum Object Oriented Programming
    Replies: 2
    Last Post: October 20th, 2009, 09:39 PM
  4. Throwing arrays as objects
    By Audemars in forum Collections and Generics
    Replies: 1
    Last Post: September 23rd, 2009, 06:29 PM
  5. [SOLVED] Creation of objects of Array in Java
    By sadi_shihab in forum Collections and Generics
    Replies: 4
    Last Post: July 9th, 2009, 01:38 PM