New To Java - How to pull data from a constructor?
Hi there, I have set up this simplified program to try and understand how to find the data of an array of an object in the main method.
I want to run a System.out.println(newPspArray[0]) from the main method but it says cannot find the variable.... I want to run this println to check if the array actually has data in it and that the object has been made
public class Demo {
public static void main(String args[]) {
}
public class Psp {
public String genre;
public String coolness;
public int rating;
public Psp() {
genre = "";
coolness = "";
rating = 0;
}
public Psp(String genre, String coolness, int rating) {
this.genre = genre;
this.coolness = coolness;
this.rating = rating;
Psp[] newPspArray = new Psp[50];
newPspArray[0] = new Psp("cool", "totallycool", 5);
}
public String getGenre() {
return genre;
}
public String getCoolness() {
return coolness;
}
public int getRating() {
return rating;
}
}
}
public class Demo {public static void main(String args[]) {
Psp[] array = new Psp[50];
array[0] = new Psp("cool", "totallycool", 5);
System.out.println(array[0]);
}
}
public class Psp {
private String genre;
private String coolness;
private int rating;
public Psp() {
genre = "";
coolness = "";
rating = 0;
}
public Psp(String genre, String coolness, int rating) {
this.genre = genre;
this.coolness = coolness;
this.rating = rating;
}
public String getGenre() {
return genre;
}
public String getCoolness() {
return coolness;
}
public int getRating() {
return rating;
}
public String toString() {
return "genre=" + getGenre() + " coolness=" + getCoolness() + " rating=" + getRating();
}
}
1. Put classes in separate files Demo.java and Psp.java. You had
accidentally nested Psp inside Demo.
2. Put some code in your main. I think the code meant for there had
been accidentally placed in the constructor.
3. To turn an object into s String, which is your intention when you pass
it to System.out.println, override toString.
yes
Demo.java
class Psp {private String genre;
private String coolness;
private int rating;
public Psp() {
genre = "";
coolness = "";
rating = 0;
}
public Psp(String genre, String coolness, int rating) {
this.genre = genre;
this.coolness = coolness;
this.rating = rating;
}
public String getGenre() {
return genre;
}
public String getCoolness() {
return coolness;
}
public int getRating() {
return rating;
}
public String toString() {
return "genre=" + getGenre() + " coolness=" + getCoolness() + " rating=" + getRating();
}
}
class Demo {
public static void main(String args[]) {
Psp[] array = new Psp[50];
array[0] = new Psp("cool", "totallycool", 5);
System.out.println(array[0]);
}
}
Even better: keep it simple. Lose class Demo entirely and stick the main into Psp:
public class Psp {public static void main(String args[]) {
Psp[] array = new Psp[50];
array[0] = new Psp("cool", "totallycool", 5);
System.out.println(array[0]);
}
private String genre;
private String coolness;
private int rating;
public Psp() {
genre = "";
coolness = "";
rating = 0;
}
public Psp(String genre, String coolness, int rating) {
this.genre = genre;
this.coolness = coolness;
this.rating = rating;
}
public String getGenre() {
return genre;
}
public String getCoolness() {
return coolness;
}
public int getRating() {
return rating;
}
public String toString() {
return "genre=" + getGenre() + " coolness=" + getCoolness() + " rating=" + getRating();
}
}