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;

}

}

}

[1124 byte] By [NocturnalManNza] at [2007-11-26 23:18:17]
# 1

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.

DrLaszloJamfa at 2007-7-10 14:20:13 > top of Java-index,Java Essentials,New To Java...
# 2
awesome thankyou! big help... the only problem is I can only hand in one java file... is there anyway around this?
NocturnalManNza at 2007-7-10 14:20:13 > top of Java-index,Java Essentials,New To Java...
# 3
remove the public modifier for Pspsave them together in one file Demo.java
rym82a at 2007-7-10 14:20:13 > top of Java-index,Java Essentials,New To Java...
# 4

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]);

}

}

fastmikea at 2007-7-10 14:20:13 > top of Java-index,Java Essentials,New To Java...
# 5

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();

}

}

DrLaszloJamfa at 2007-7-10 14:20:13 > top of Java-index,Java Essentials,New To Java...
# 6
wow awesome help !!!! I understand the fundamentals way better now..time to get coding
NocturnalManNza at 2007-7-10 14:20:13 > top of Java-index,Java Essentials,New To Java...