d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Quick Java Question! > Yeah
Add Reply New Topic New Poll
Member
Posts: 1,756
Joined: Sep 21 2013
Gold: 1,645.00
Jul 3 2016 11:42am
I am not sure if the "new" keyword is needed here in this case, I'm pretty new to Java

private String editDistOne(String inputWord){

Node x;

x = (some function that returns a Node object);

can I now use the variable "x" as I would any other node object? Or do I need to say "Node x = new node" at the top?


}
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jul 3 2016 11:48am
the `new` keyword creates a new object. since your function is retrieving (possibly also creating) your object, the `new` keyword is not needed here. it's probably inside that function somewhere down the rabbit hole.

Quote
Or do I need to say "Node x = new node" at the top?


there is no need to do that if you're assigning x to hold another object later prior to using it.

This post was edited by carteblanche on Jul 3 2016 11:48am
Member
Posts: 1,756
Joined: Sep 21 2013
Gold: 1,645.00
Jul 3 2016 11:49am
Quote (carteblanche @ Jul 3 2016 11:48am)
the `new` keyword creates a new object. since your function is retrieving (possibly also creating) your object, the `new` keyword is not needed here. it's probably inside that function somewhere down the road.



there is no need to do that if you're assigning x to hold another object later prior to using it.


Thanks a ton man, that completely answers my question. Cheers
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll