answersLogoWhite

0


Best Answer

this is one of the keyword in java programming language. this can be used inside any method to refer to the current object. example: class Box{ double width; double height; double depth; Box(double w,double h,double d){ this.width=w; this.height=h; this.depth=d; }

} The significance of the keyword this would be realized in cases where we use inheritance. There may be cases where you have methods of the same name in both the current class and its parent. That is when the keyword "this" can be used to tell the JVM to invoke the method of the current class and not the parent class. this refers to the object with which the method inside a class is called. For instance, I call studentObject.getGrade(), in the getGrade() method to call my own method or variables, i.e. a private instance variable named grade, I would state: this.grade. Though inside the class any method or variable that is called without an object automattically calls the most local method or variable, i.e. I could just call grade for the above example given that I do not have a variable of the same name declared in the method. "this" is useful in those circumstances where there are more than one variable with the same name, just for example that I do have a variable named grade in my getGrade() method, to get my instance variable for my object I would simply call this.grade, thus to be safe use this for instance variables.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

This keyword holds the reference of the current object . for example

if we do like:-

Ram r= new Ram(2); //Ram is a class here

here 2 is passed in the constructor and it will be hold like:-

Ram(int a)

{

this.b=a; //int b is a variable here

}

then here "this" will hold the reference of r and

internal working of this is like:-

Ram r=new Ram(r,2);

and holds like

Ram(this d, int a)

{

d.b=a;

}

//if you didn't got it contact me :- kumar976001@gmail.com

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

abstractassertbooleanbreakbytecasecatchcharclassconstcontinuedefaultdodoubleelseenumextendsfinalfinallyfloatforgotoifimplementsimportinstanceofintinterfacelongnativenewpackageprivateprotectedpublicreturnshortstaticstrictfpsuperswitchsynchronizedthisthrowthrowstransienttryvoidvolatilewhile

note : some of this word will be used in laret versions.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

The keyword 'this' refers to the current instance. You can access any member of the class using this.<member name>. For example, if you have an Employee class with members 'firstName' and 'lastName' and let's say you have a private method that formats a full name everytime there is a change in either the first name or the last name. Then the formatFullName method can access the mebers using 'this' keyword as follows:

private void formatFullName() {

this.fullName = String.format("%s %s", this.firstName, this.lastName);

}

Note that however the use of keyword 'this' is not required in this example. An example where use of this keyword is necessary is when calling another contsrictor of the same class.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

The this keyword is used to refer to the current object instance in Java.

Let us take an example where this keyword would be useful. Assume you have Class A that extends Class B. Both classes have a method getName()

Now if you want to specifically call the getName() method of the current object from inside Class B you can do it by this.getName().

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

The this keyword references the object class itself. If you wanted to perform an action on the class itself, you would use this, since you can not create an instance of yourself.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

string

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Explain this keyword with example in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Is check a keyword in java?

No, 'check' is not a keyword in java language.


Is float is a keyword in java?

yes, float is keyword and data type in java


What does the verify keyword do in Java?

"verify" is not a Java keyword. I believe the link, in related links, has the complete list of Java keywords.


What is foreign keyword in java?

There is no "foreign" keyword in Java, however, there is a native keyword that declares native methods in a native language, such as C or C++.For full list of keywords in Java see related question.


What is literal in java programming?

Literal in java are L, F, null, true, false These act as keyword(have special meaning in java) but these does'nt comes under the category of Java Keyword.


What is java's keyword for the integer data type?

"int" is the keyword for integer


How does the final keyword affect the behavior of Object type in Java?

In Java, the final keyword specifies that the object created cannot be further redefined or derived.


What do you mean by this keyword in java?

"this" is a Java keyword that references the current object. Any part of the object(instance variables, methods, constructors) can be accessed by calling this.[member].


Is sizeof keyword in Java?

sizeof is not a keyword in Java but many classes have size() or length() methods, which can mean the number of elements, characters, etc. depending on the class.


What is a new keyword in java methods?

new is a keyword to create a instance of object any class.


How do you throw exception in java?

we do it using the throw keyword.


How java use extern key word which is used in C plus plus?

No extern keyword in Java.