1

I'm new in java and I saw this sample code. I don't know why in JavaApplication.java file we need to create a new instance by new keyword to set the goat name but in Tiger.java there is no need to create a new instance by new keyword to set the goat name! what's the difference?

JavaApplication.java

public static void main(String[] args) {
    Tiger t = new Tiger();
    Goat g = new Goat();
    Goat g1 = new Goat();
    g.name = "goaty";
    g1.name = "goatia";
    t.name = "woofy";
    t.hunt(g);
    t.hunt(g1);
}

Tiger.java

public class Tiger {
    String name;
    void hunt(Goat food) {
        System.out.println(name + " ate " + food.name);
    }
}

Goat.java

public class Goat {
    String name;
}
8
  • The client is directly accessing the field name. Commented Mar 2, 2019 at 6:37
  • name is class level default variable in Tiger,java whereas, in JavaApplication.java, you'll have to instantiate the class to use name property of Tiger class. It's the fundamental OOPS principle. Commented Mar 2, 2019 at 6:42
  • It is not really clear where you are struggling. If there is already an instance why create another one? Commented Mar 2, 2019 at 6:42
  • I mean the goat name @Abhinav Commented Mar 2, 2019 at 6:50
  • 2
    In void hunt(Goat food) {...} method in Tiger.java, you aren't creating a new Goat object. Instead, you're expecting a Goat object (created somewhere) to be passed on to hunt method. Commented Mar 2, 2019 at 6:57

4 Answers 4

2

In Tiger class inside the function hunt, food is a parameter of type Goat and a parameter doesn't need to be instantiated by the new keyword. Only the object needs to be instantiated.

Sign up to request clarification or add additional context in comments.

2 Comments

"and a parameter doesn't need to be instantiated by the new keyword" -- To be more correct, the parameter variable does at some point need to be instantiated, its just that this instantiation is performed prior to calling the method.
What if the food is null? It will throw NPE. You should make a fail-safe method and instantiate food if it is null or check food before proceeding ;)
1

I'm also new at java but what I understand in java is that you can't link non-static method/variable to a static one and in order to do so, you need to create an instance for it. :) the new keyword is needed to create an instance of it btw..

1 Comment

Good understanding you have.
1

As far as i can see you have placed Goat food as parameter which means hunt method will accept an object of type Goat basically food is reference variable of type Goat.So any thing that a Goat can do should apply to food also.In your Goat class you have only a field of name .So whenever you create an object you can just give it a name that's it .Now you are passing Goat object into your method so in order to printout food.name your goat needs to have a name first.

Comments

1

A Goat can be created only by using the 'new' keyword. After a goat is created you can give this goat to a tiger to eat. So this goat here

 void hunt(Goat food) {
    System.out.println(name + " ate " + food.name);
}

is already created so you don't have to create it again. Of course you can initate it again but it would be pointless since if a tiger could create a goat by itself it would not need to hunt goats.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.