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: HOW TO USE THE OPERATOR "this" in JAVA(OOP)?

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation HOW TO USE THE OPERATOR "this" in JAVA(OOP)?

    What is the purpose of this operator?
    What is this operator?
    Give me codes with "this" operator.
    When to use it?
    HELP ME WITH THIS!


  2. #2
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default Re: HOW TO USE THE OPERATOR "this" in JAVA(OOP)?

    The this operator is used to refer to the current context - in other words, the object that you are dealing with right now.

    Consider a class called Person that expects some parameters like name, age during instantiation, you can then set those properties by calling this.property = property_passed_in to the constructor.

    public Class Person{
        private String name;
        private int age;
     
        public Person(String name, int age){
            this.name = name;
            this.age = age;
        }
    }
    When the Person object is created, its properties name and age will be set. I hope this helps.

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

    crys (August 29th, 2013)

  4. #3
    Member
    Join Date
    Jul 2013
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: HOW TO USE THE OPERATOR "this" in JAVA(OOP)?

    Think of any dot-separated operators you add before a var name in the context of an ascending pyramid.

    If you have a method in a class, and you refer to x, that's x in the method.

    If you refer to this.x, that's the x in the class.

    If you refer to anotherClass.x, that's x in another class.

    No operators = closest x. Then comes this, then comes specifying where this "x" you speak of is.

    -s45

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: HOW TO USE THE OPERATOR "this" in JAVA(OOP)?

    As mentioned above the this operator is used to refer to the current object. Another use is when you need to call a method in another class that has a parameter of the current class.
    class Bar {
        public void doStuff(Foo f) {
     
        }
    }
     
    class Foo {
        private Bar b = new Bar();
     
        public void someMethod() {
            b.doStuff(this);
        }
    }
    Improving the world one idiot at a time!

Similar Threads

  1. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  2. Replies: 8
    Last Post: May 6th, 2013, 07:53 AM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM
  5. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM