Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to access non static cloned arrayList for use in static method in Java
    text
    copied!<p>As part of an assignment Im trying to access a cloned array list from another class so I can utilize it. But when attempting to do so I get the following error "non-static method getConnections() cannot be refrenced from a static context". </p> <p>This is the code I'm using to access the cloned array. It is in the context of working out the best way to take flights from one destination to another.</p> <pre><code>public boolean determineRoute(City from, City to, ArrayList&lt;City&gt; flightRoute) { ArrayList&lt;City&gt; Connections = new ArrayList&lt;City&gt;(); Connections = City.getConnections(); return true; } </code></pre> <p>And this is how the code for that class begins. It does start as static but as far as i can see it should only affect the first method how can I tell java that this method should not be considered static so I can access the cloned list from the non static class??</p> <pre><code>import java.util.*; public class Lab9_Ex2_Main { //////// START-UP ///////// public static void main(String[] args) { new Lab9_Ex2_Main(); } </code></pre> <p>I have left out a lot of the code as I think it may not be right from me to put every thing up. But should you need more to get a clearer picture I will happily add more of the code.</p> <p>This is the code from another class which contains a cloned array which im attempting to access.</p> <pre><code>import java.util.*; // Class: City // Purpose: To represent a place in the world that you can fly from/to. public class City { private String name; // The name of the City private ArrayList&lt;City&gt; connectsWith; // Which cities are connected to this one public City(String cityName) { name = cityName; connectsWith = new ArrayList&lt;City&gt;(); } // Method: addConnection // Purpose: To note that you can catch a flight to the destination, from this city // Passed: // destination - The City which you can fly to. public void addConnection(City destination) { if (destination != null &amp;&amp; destination != this) connectsWith.add(destination); } // Method: getConnections // Purpose: To retrieve a list of cities you can reach from this one. // Note: You are given a clone, (to avoid a privacy leak), and can manipulate it however // you like. E.g. you could delete elements. public ArrayList&lt;City&gt; getConnections() { return (ArrayList&lt;City&gt;) connectsWith.clone(); } public String getName() { return name; } public String toString() { return name; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload