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 8998473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:02:10+00:00 2026-06-16T00:02:10+00:00

The following unexpectedly compiled and ran without a problem: import info.gridworld.actor.Actor; import java.util.ArrayList; public

  • 0

The following unexpectedly compiled and ran without a problem:

import info.gridworld.actor.Actor;
import java.util.ArrayList;

public class ChameleonKid extends ChameleonCritter
{
    public ArrayList<Actor> getActors()
    {
        ArrayList<Actor> actors = getGrid().getNeighbors(getLocation());
          ArrayList<Actor> frontBack = new ArrayList<Actor>();
          for(Actor i : actors)
              if(getLocation().getDirectionToward(i.getLocation())==getDirection())
                    frontBack.add(i);
        return frontBack;
    }
}

The method getLocation() in the Actor class returns an instance of Location. And then I call the getDirectionToward() method of the Location class. getLocation().getDirectionToward(i.getLocation()). How does this work? I never imported the Location class. How am I able to work with it and call its methods? If that is how it works, when would I need to import a class? Only if I am instantiating it?

I am using Java 7.

  • 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-06-16T00:02:11+00:00Added an answer on June 16, 2026 at 12:02 am

    Say you have two methods, one returning foo.Location and the other returning bar.Location (two completely different classes happening to have the same name, but in different packages – completely valid):

    foo.Location getFooLocation();
    bar.Location getBarLocation();
    

    When you can both of these methods in the same class and chain some methods, you don’t need an import:

    if(getFooLocation().onlyInFoo()) {
      //...
    }
    if(getBarLocation().onlyInBar()) {
      //...
    }
    

    It works because the compiler is completely sure which version (from which package) of Location are you using and it knows where onlyInFoo() and onlyInBar are available.

    But suppose you need a local variable:

    Location location;
    // much later
    location = getFooLocation();
    

    Now the compiler doesn’t really know Location do you mean, so you must help him either by proceeding class name with package:

    foo.Location location;
    

    or by importing that class:

    import foo.Location;
    

    You should now ask: what if I want to have local variable of both foo.Location and bar.Location? Well, you can’t import them both, obviously:

    //WRONG
    import foo.Location;
    import bar.Location;
    

    What you can do is again: either don’t import at all and use fully qualified names:

    foo.Location location1;
    bar.Location location;
    

    …or import just one location:

    import foo.Location;
    
    //...
    Location location1;     //foo.Location
    bar.Location location2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following definitions: template<typename T1, typename T2> class Test2 { public: static
I tried with following regex, but it didn't work. myString.replaceAll(\, /); Exception: java.util.regex.PatternSyntaxException: Unexpected
he following compiles: import scala.collection.mutable.MutableList abstract class GeomBase[T <: DTypes] { val grids =
I have a problem with the following HQL query: select sum(MYTABLE.COUNTER) from ( select
In F#, given the following class: type Foo() = member this.Bar<'t> (arg0:string) = ignore()
I have the following code which compiles without errors under Linux and Mac OS
After several hours of experimentation, I've managed to reduce the problem to the following
Having a problem with TFS and TFS CI in the following scenario (I have
Am trying to run the following python program import re regex=re.compile(http...imgs.xkcd.com.comics.[\\S]*.[jpg|png]) f=open('out.txt') for a
While running following code, my program crashes unexpectedly! #include<stdio.h> #include<string.h> int main(){ char *str

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.