Reputation: 55
Every time I create a new Dog object on a main Java class, I am unable to replace "null" (which represents the String Name variable from the Dog class) with the string I want. Here is the Class:
private String Name;
private int Age;
public Dog(String Name, int Age) //Constructor {
this.Name =(String) Name;
this.Age = Age;
}
public int getAge() {
return Age;
}
public void setAge(int Age) {
this.Age = Age;
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
Upvotes: 0
Views: 194
Reputation: 1180
Try Dog dog1 = new Dog("James", 2);
instead. The string literal has to be enclosed in double quotes, else, it'll be treated as a variable.
or, add
String James = "Name of the dog";
before instantiating the Dog with Dog dog1 = new Dog(James, 2);
Upvotes: 1
Reputation: 231
nice :)
public Dog(String Name, int Age) //Constructor {
http://java.sun.com/docs/codeconv/html/CodeConventions.doc4.html
Upvotes: 0
Reputation: 31453
well, you have variables starting with Capitals, you have commented out a bracket at the constructor definition, you do unnecessary String casts...
Possible fixing option -> create your class like this:
public class Dog {
private String name;
private int age;
}
Then, from your IDE, go to code generation options -> Choose generate getters and setters for both fields. Choose constructor generation with parameters. Done.
Upvotes: 2
Reputation: 3914
You made your opening brace of your constructor to a comment:
public Dog(String Name, int Age) //Constructor { <-- Brace is part of comment
Solution:
public Dog(String Name, int Age) /*Constructor*/ {
Upvotes: 6