Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6589343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:09:42+00:00 2026-05-25T17:09:42+00:00

As part of an assignment Im trying to access a cloned array list from

  • 0

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”.

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.

public boolean determineRoute(City from, City to, ArrayList<City> flightRoute)
{
    ArrayList<City> Connections = new ArrayList<City>();
    Connections = City.getConnections(); 
    return true;
}

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??

import java.util.*;


public class Lab9_Ex2_Main
{
    //////// START-UP /////////
    public static void main(String[] args)
    {
        new Lab9_Ex2_Main();
    }

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.

This is the code from another class which contains a cloned array which im attempting to access.

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<City> connectsWith;       // Which cities are connected to this one

    public City(String cityName)
    {
        name = cityName;
        connectsWith = new ArrayList<City>();
    }

    // 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 && 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<City> getConnections()
    {
        return (ArrayList<City>) connectsWith.clone();
    }

    public String getName()
    {
        return name;
    }

    public String toString()
    {
        return name;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:09:43+00:00Added an answer on May 25, 2026 at 5:09 pm

    City actually doesn’t provide a static getConnections() method, since that doesn’t make sense. The connections depend on an actual City instance and if you have access to one you can call getConnections() on it, even from a static method.

    This is the comment on the array list that is cloned in getConnections():

    // Which cities are connected to this one

    Note that this means you just can’t get the connections without specifying this city (the one you get the connections for) and thus just can’t call that method on the City class only.

    Comment on the method itself:

    Purpose: To retrieve a list of cities you can reach from this one.

    Assuming your determineRoute(...) method might be static, it could look like this:

    public static boolean determineRoute(City from, City to, ArrayList<City> flightRoute)
    {
       ArrayList<City> connections = new ArrayList<City>();
       connections = to.getConnections(); //or from.getConnections(); what ever makes sense
    
       //connections is not used, so I assume you want to put them into flightRoute
       flightRoute.addAll(connections);
    
       return true; 
    }
    

    Your determineRoute(...) logic seems quite odd. I assume you want to actually calculate the route between the from and the to city, which you are not doing right now. Fixing that, however, is an exercise for you.

    You could then call that method in your main method (which has to be static) like this:

    public static void main(String... args) {
      City berlin = new City("Berlin");
      City beijing = new City("Beijing");
    
      //fill the connections here
    
      ArrayList<City> route = new ArrayList<City>();
    
      boolean success = determineRoute(berlin, beijing, route);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing the SaaS Stanford class, trying to do Part 5 of this assignment
As part of a python assignment I'm trying to implement list sorting (ascending) without
Im having a problem with a final part of my assignment. We get in
As part of the last assignment in a beginner python programing class, I have
I have an assignment in a language-independent class, and part of it is using
as a part of a homework assignment, I'm trying to read a single char
I'm trying to do a little more than required for an assignment in class.
Hi I am new to Python and as a part of my assignment I
As part of a homework assignment in Lisp, I am to use apply or
This is part of a homework assignment. I've got several questions asking find the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.