0

I have 2 classes, Line and Visual. Line has a constructor with 2 parameters and Visual contains the main method and a public static void showEle(ELe e){}

Line also has an arrayList called line that is private, so I have create a get method for that:

public ArrayList<Passenger> getLine(){
    return line;
}

Problem arises when I try to call getLine() using line.getLine(), says "cannot find symbol".

public Line obj;   <----this is outside showEle method but same class.

The following in showEle method of Visual:

obj.getLine(); 

but i end up with "non static variable obj cannot be referenced from a static context".

Another way I can do is creating an object but that would require putting parameters because of the constructor in class Line.

Can't think of any other ways to call getLine() method in class Line from a static method in another class.

Code:

public class Visual {

    public static void showEle(Ele e){  
        line.getLine();
    }

    public static void main(String[] args){
    }      
}

public class Line {

    private ArrayList<Passenger> line = new ArrayList<>(); 

    public ArrayList<Passenger> getLine(){
        return line;
    }
}
rated2016
  • 153
  • 1
  • 2
  • 12
  • 2
    Don't just describe how you wrote your code. Actually post it here so we can see exactly what you did. – azurefrog Feb 17 '16 at 22:48
  • 2
    Other potential duplicate (hard to tell without proper code example): [non-static variable cannot be referenced from a static context](http://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context) – Pshemo Feb 17 '16 at 22:50
  • The issue is with your mixed use of static methods and member variables – Andrew Williamson Feb 17 '16 at 22:50
  • I've updated with the important pieces of code. Sorry, this is my first post. – rated2016 Feb 17 '16 at 22:55
  • 2
    To call `getLine()` on a `Line` object, you first need to create a `Line` object. – Louis Wasserman Feb 17 '16 at 22:57

0 Answers0