New Posts  All Forums:Forum Nav:

Java this. command

post #1 of 5
Thread Starter 
I have a quick question.


when I use something like this.employee


where does this look? is it depending on the method im in? or the class? or Constructor?


say I have

Class Office

methods named duty, people, desks

and i make a new method called outside

Code:
public static void people {


public int employee = 3;



}


how can I use the this command to call employee inside people?

can I use

this.employee?
My System
(13 items)
 
  
CPUMotherboardGraphicsOS
Opty 170 CCBWE 0550 UPMW (2.6GHZ) Asus A8N32 Deluxe ATI X1900XT 648/774 Windows XP, Vista, Linux Ubunutu 
MonitorKeyboardPowerCase
17" CRT Zboard Merc Seasonic S12-600HT 600W Antec P180 
Mouse
Logitech G7 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsOS
Opty 170 CCBWE 0550 UPMW (2.6GHZ) Asus A8N32 Deluxe ATI X1900XT 648/774 Windows XP, Vista, Linux Ubunutu 
MonitorKeyboardPowerCase
17" CRT Zboard Merc Seasonic S12-600HT 600W Antec P180 
Mouse
Logitech G7 
  hide details  
Reply
post #2 of 5
The this keyword is used to refer to the object itself.

Suppose you have a class named Employee, with two members: name, a field; setName, a method.
Code:
class Employee {
    private String name;

    public void setName(String name) {
        this.name = name;
    }
}

The reason we use this is because the method parameter hides the object's member variable. Some people tend to use it redundantly in order to reduce ambiguity.

I hope that helps.
Nehalem
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 920 Asus P6T XFX Radeon HD 4890 A-Data 3x2GB DDR3 DIMM 
Hard DriveHard DriveOptical DriveCooling
Samsung Spinpoint F3 Samsung EcoGreen F4 Samsung DVD Writer Thermaltake Frio 
OSOSMonitorKeyboard
Windows 7 64-bit Arch Linux 64-bit ASUS VH222D A4Tech Generic 
PowerCaseMouseAudio
Thermaltake Toughpower 650W Thermaltake M5 Logitech MX518 Asus Xonar DX 
  hide details  
Reply
Nehalem
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 920 Asus P6T XFX Radeon HD 4890 A-Data 3x2GB DDR3 DIMM 
Hard DriveHard DriveOptical DriveCooling
Samsung Spinpoint F3 Samsung EcoGreen F4 Samsung DVD Writer Thermaltake Frio 
OSOSMonitorKeyboard
Windows 7 64-bit Arch Linux 64-bit ASUS VH222D A4Tech Generic 
PowerCaseMouseAudio
Thermaltake Toughpower 650W Thermaltake M5 Logitech MX518 Asus Xonar DX 
  hide details  
Reply
post #3 of 5
Thread Starter 
Quote:
Originally Posted by Harbinger View Post

The this keyword is used to refer to the object itself.
Suppose you have a class named Employee, with two members: name, a field; setName, a method.
Code:
class Employee {
    private String name;
    public void setName(String name) {
        this.name = name;
    }
}
The reason we use this is because the method parameter hides the object's member variable. Some people tend to use it redundantly in order to reduce ambiguity.
I hope that helps.



so in the case above the setName would be the object?

I always thought objects were only created like

employee jim = new employee();


then the object would be jim



also, does this have to be used only in the same method as the object? what if I have multiple objects how can I direct this to the correct one?
My System
(13 items)
 
  
CPUMotherboardGraphicsOS
Opty 170 CCBWE 0550 UPMW (2.6GHZ) Asus A8N32 Deluxe ATI X1900XT 648/774 Windows XP, Vista, Linux Ubunutu 
MonitorKeyboardPowerCase
17" CRT Zboard Merc Seasonic S12-600HT 600W Antec P180 
Mouse
Logitech G7 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsOS
Opty 170 CCBWE 0550 UPMW (2.6GHZ) Asus A8N32 Deluxe ATI X1900XT 648/774 Windows XP, Vista, Linux Ubunutu 
MonitorKeyboardPowerCase
17" CRT Zboard Merc Seasonic S12-600HT 600W Antec P180 
Mouse
Logitech G7 
  hide details  
Reply
post #4 of 5
The "this" keyword in Java (like Harbinger mentioned) references the current object the call is made in. I.E.
Code:
class ThisClass
{
public int x;

//Class constructor.
public ThisClass(int x, int y)
{
     //This call refers to X in this function.
     this.x  =  x;
}

}

Oracle gives a pretty good description of it: http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html . Java is not my main language so I hope that helps.
Edited by BinaryBoogieman - 4/8/12 at 12:56pm
NewEden
(16 items)
 
  
CPUMotherboardGraphicsRAM
AMD Athlon(tm) II X4 640 MSI 880GM-E41 EVGA 550 TI Superclocked GSkill RipJaw 
Hard DriveCoolingCoolingOS
Patroit Pyro Stock CPU Air Windows 7 Ultimate x64 
OSMonitorMonitorKeyboard
Ubuntu 11.10 x86 Dell 19-inch LCD JVC 39-inch LCD 1988 IBM Model M 
PowerCaseMouseAudio
Kingwin 730 Watt EarthPower Unknown Razor Tron series Onboard 7.1 
  hide details  
Reply
NewEden
(16 items)
 
  
CPUMotherboardGraphicsRAM
AMD Athlon(tm) II X4 640 MSI 880GM-E41 EVGA 550 TI Superclocked GSkill RipJaw 
Hard DriveCoolingCoolingOS
Patroit Pyro Stock CPU Air Windows 7 Ultimate x64 
OSMonitorMonitorKeyboard
Ubuntu 11.10 x86 Dell 19-inch LCD JVC 39-inch LCD 1988 IBM Model M 
PowerCaseMouseAudio
Kingwin 730 Watt EarthPower Unknown Razor Tron series Onboard 7.1 
  hide details  
Reply
post #5 of 5
Quote:
Originally Posted by xsabrewulf View Post

so in the case above the setName would be the object?
I always thought objects were only created like
employee jim = new employee();
then the object would be jim
also, does this have to be used only in the same method as the object? what if I have multiple objects how can I direct this to the correct one?

No, the object is an instantiated Employee class, as you already know. this always refers to the current object, no matter where you use it. So if you have two objects with methods using the this keyword, they will refer to themselves, not to any external object.

If you wish to manipulate (or call any methods of) one object via another, then you need to pass that object as a reference.

Using the previous example, if you wanted to call an Employee object's setName method from another object, you would do something like this:
Code:
public static void main(String args[]) {

    Employee john = new Employee();
    MyClass manipulator = new MyClass();

    manipulator.doStuffOnEmployee(john);
}

where the doStuffOnEmployee method of the MyClass object simply takes an Employee object as its argument and in the method body, makes a call to the passed object's setName method.
Code:
public void doStuffOnEmployee(Employee x) {
    x.setName("John");
}
Nehalem
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 920 Asus P6T XFX Radeon HD 4890 A-Data 3x2GB DDR3 DIMM 
Hard DriveHard DriveOptical DriveCooling
Samsung Spinpoint F3 Samsung EcoGreen F4 Samsung DVD Writer Thermaltake Frio 
OSOSMonitorKeyboard
Windows 7 64-bit Arch Linux 64-bit ASUS VH222D A4Tech Generic 
PowerCaseMouseAudio
Thermaltake Toughpower 650W Thermaltake M5 Logitech MX518 Asus Xonar DX 
  hide details  
Reply
Nehalem
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 920 Asus P6T XFX Radeon HD 4890 A-Data 3x2GB DDR3 DIMM 
Hard DriveHard DriveOptical DriveCooling
Samsung Spinpoint F3 Samsung EcoGreen F4 Samsung DVD Writer Thermaltake Frio 
OSOSMonitorKeyboard
Windows 7 64-bit Arch Linux 64-bit ASUS VH222D A4Tech Generic 
PowerCaseMouseAudio
Thermaltake Toughpower 650W Thermaltake M5 Logitech MX518 Asus Xonar DX 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming