I am using bluej to make a program for userlists. When i compile and run the code bluej does not give anny errors. But when i make a new instance of a class it looks like nothing happens. Printing a line to my terminal window from my instance does not work. How can i make my instance print show in my terminal window?
import java.util.*;
import java.text.*;
public class Main{
private ArrayList<List> userlists;
public Main(){
System.out.print('\f');
System.out.println("this text will show.");
newlist("listname");
}
public void newlist(String listname){
System.out.println("this text will show too!");
List userlist = new List(listname); //terminal does not show lines printed by constructor of List?
userlists.add(userlist);
userlist.printSomeText(); //second attempt to print a line, does not show in terminal.
}
}
public class List {
private String listname;
public List(String ln) {
listname = ln;
System.out.println("this text does not show.");
}
public void printSomeText(){
System.out.println("this text neither.");
}
}
I Run this by (first compiling both classes and then) rightclicking on the Main class in bluej's interface and choosing new Main(). When i do this a terminal window shows up showing:
this text will show.
this text will show too!
but it does not show:
this text will show.
this text will show too!
this text does not show.
this text neither.
It does not show anny error's so i would like to know whats going wrong and how to get the second result, showing those four lines.
First of all you dont have two public
class in the same Java
file.
Second you must have to initialize the variable before using that variable or field, which is not happening in your question. I have done some modification in your code that will work according to you and also print the second bunch of lines, which you want to print.
import java.util.ArrayList;
public class Main {
private ArrayList<List> userlists;
public Main() {
userlists = new ArrayList<List>();
System.out.print('\f');
System.out.println("this text will show.");
newlist("listname");
}
public void newlist(String listname) {
System.out.println("this text will show too!");
List userlist = new List(listname); // terminal does not show lines
// printed by constructor of List?
userlists.add(userlist);
userlist.printSomeText(); // second attempt to print a line, does not
// show in terminal.
}
public static void main(String ... args){
new Main();
}
}
class List {
private String listname;
public List(String ln) {
listname = ln;
System.out.println("this text does not show.");
}
public void printSomeText() {
System.out.println("this text neither.");
}
}