Object not being read in another class - Java
Object not being read in another class - Java
I apologize if this has been asked before.
Here are 2 files that I have: Dog.java, and DogTest.java.
Dog.java:
public class Dog
String name;
public Dog(String name)
System.out.println("Name chosen is : " + name);
public static void main(Stringargs)
Dog dog1 = new Dog("Big Larry");
Dog dog2 = new Dog("Mr. Cuddles");
public void bark()
System.out.println(name + " is barking.");
public void eat()
System.out.println(name + " is eating.");
public void sleep()
System.out.println(name + " is sleeping.");
And here is DogTest.java:
public class DogTest
public static void main(String args)
dog1.bark();
dog2.bark();
dog1.eat();
dog2.sleep();
dog1.sleep();
Now, in the Dog java file, it shows as that dog1 and dog2 were not initialized, as said in my IDE. Yet, in the DogTest file, I put the object variables there. What I don't understand, is why are they not initialized?
If I re-initialize them again in the DogTest.java file, DogTest compiles, with no errors, but it calls them "null" in the output when they are supposed to read,"Big Larry is barking." or "Mr.Cuddles is sleeping.", instead of "null is barking."
I would like to understand this. Thank you very much for any help, and have a good day! Please leave any questions below in the comments.
Dog dog1 = new Dog();
4 Answers
4
Here is your problem:
public Dog(String name)
System.out.println("Name chosen is : " + name);
You pass the name to your constructor, but you don't actually set the value.
Change this to:
public Dog(String name)
System.out.println("Name chosen is : " + name);
this.name = name;
Not sure why you need the print statement there.
Also, in your test class, don't forget to actually declare and initialize the variables:
Dog dog1 = new Dog("Max");
dog1.bark();
They weren't initialized in your test class, because they were initialized in the local scope of the main method, which was/is out of reach for your test class.
I did initialize them in the DogTest file, but took them out before I posted. So I need to put them back?
– Yoshi24517
Aug 30 at 22:36
if you want to keep that test class, yes. But do you really need that class? you can just use the main method in your Dog class to check the behaviour.
– Stultuske
Aug 31 at 6:03
I had to keep them separate for the class this assignment was for.
– Yoshi24517
Sep 1 at 17:54
You have two differnt files Dog and DogTest
DogTest have no access to Dog1 and dog2 variable which are in Dog.java - main() method hence you have to initialize in DogTest as well
You can remove main method from Dog.java class
You need to change your DogTest as
public class DogTest
public static void main(String args)
Dog dog1 = new Dog("Big Larry");
Dog dog2 = new Dog("Mr. Cuddles");
dog1.bark();
dog2.bark();
dog1.eat();
dog2.sleep();
dog1.sleep();
Also you need to change constructor of Dog.java to
public Dog(String name)
this.name =name
System.out.println("Name chosen is : " + name);
you forgot about the null-issue
– Stultuske
Aug 30 at 7:12
@Stultuske Thanks for pointing out updated the solution
– Rupesh Agrawal
Aug 30 at 7:12
You initialized in one file and trying to access in another file which will be a compilation error. Actually, you don't need two files. Since you have a main method in the Dog file you can test dogs there itself like below.
Also, you need to assign the name to the field.
public class Dog
String name;
public Dog(String name)
this.name = name;
System.out.println("Name chosen is : " + name);
public static void main(String args)
Dog dog1 = new Dog("Big Larry");
Dog dog2 = new Dog("Mr. Cuddles");
dog1.bark();
dog2.bark();
dog1.eat();
dog2.sleep();
dog1.sleep();
public void bark()
System.out.println(name + " is barking.");
public void eat()
System.out.println(name + " is eating.");
public void sleep()
System.out.println(name + " is sleeping.");
For my class, we needed 2 files.
– Yoshi24517
Aug 30 at 22:33
Please find fix code below :
public class Dog
String name;
public Dog(String name)
System.out.println("Name chosen is : " + name);
this.name = name;
public static void main(String args)
Dog dog1 = new Dog("Big Larry");
Dog dog2 = new Dog("Mr. Cuddles");
public void bark()
System.out.println(this.name + " is barking.");
public void eat()
System.out.println(this.name + " is eating.");
public void sleep()
System.out.println(this.name + " is sleeping.");
Test class:
public class DogTest
public static void main(String args)
Dog dog1 = new Dog("Goffy");
Dog dog2 = new Dog("Cherry");
dog1.bark();
dog2.bark();
dog1.eat();
dog2.sleep();
dog1.sleep();
Before using variable you have to initialize it in 1st stage, you had just pass the value in constructor but you had not assign it to class variable, also please read about **THIS** keyword and it's importance in java.
**THIS**
THIS: https://www.guru99.com/java-this-keyword.html
Cheers
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Dog dog1 = new Dog();– Guy
Aug 30 at 7:03