what is use of new and this keyword in java

Recommended Answers

All 3 Replies

The keyword this is useful when you need to refer to instance of the class from its method. The keyword helps us to avoid name conflicts.

The keyword new in Java that allocates new objects and initialises them.

gabhinav, I suggest you don't waste the time to drop these type of posts here. This forum was sincerely help to others while developing the code or any typical issues and bugs etc., don't you got this and new usages in google. Google it man. Please click on the " Mark this thread as solved" which is shown in the below or delete it before you got a bad reputation. I hope you understood.

VirtualAsset's defintions are correct. Examples might help, though:

"This" would often be used in a constructor or in a setter method. You'll often see it used like this:

public class MyClass{

String someField;
public MyClass(String someField)
{
this.someField = someField;
}

public void setSomeField(String somefield)
{
this.someField = someField;
}

To understand this, you have to understand shadowing. If a method declares a variable with a name that's used as a field of the class (ie, takes a parameter called "someField" when there's already a "someField" existing in the class scope), there will be two different variables with that name, and the local one willshadow the class-level one. A reference to "someField" will affect the method-level "someField", not the one at the class level. But what if you want to work with the class-level one - for example, what if you want to set a class field? For that you use the "this" keyword, which refers to the current instance of the class. If you have a MyClass object called mc, "this.someField" refers to mc.someField, even if you're in a local context that otherwise shadows someField.

That's probably the most common use of "this". Another which I've come across would be when you want to create an object that can refer back to the current one. For example, if you have an applet that's creating a Model, a View, and a Controller*, you want those to be able to call methods of the applet, you might pass a reference to the applet in via the constructor. That's when you'd use "this":

Model m = new Model (this);
View v = new View (this);
Controller c = new Controller (this, m, v);

Now each of those objects can reach back up to the applet that spawned them, perhaps to get parameters or call utility methods, and the Controller can pass information to the Model and the View.

I hope that helps with "this".

"New" is easier, it just instantiates an object.

Dog fido = new Dog("Fido", "boxer");

calls Dog's constructor with those two strings as parameters and passes fido a pointer to the resulting object.


*Model/View/Controller is a very useful design pattern - if it's new to you, please look it up before you ask about it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.