Rik Nijhuis
Rik Nijhuis

Reputation: 109

not importing from another class

Okay, I've got the following code:

package executive;

import animals.*;

public class MainFile {

    public static void main(String[] args) {

        dog Robbie;
        Robbie = new dog();

        Robbie.lick();
        Robbie.jump();


    }

}

In the package animals I've got the class dog (Yes I know it's a really useless program):

package animals;

public class dog {

    void lick() {
        System.out.println("lick lick");
    }
    void jump() {
        System.out.println("Whihoooo");
    }
}

But if I'm running this code (eclipse) I get an error: "The method lick() from the type dog is not visible The method jump() from the type dog is not visible"

The most people with this problem haven't made the class public, but I have. The code is working well at the moment I'm putting the dog-class in the same package.

Upvotes: 0

Views: 82

Answers (6)

Nate
Nate

Reputation: 16888

Java Access Modifiers

Methods need access modifiers as well as classes.

If you leave off the access modifier the default is called "package private" which means only classes in the same package can call the method.

If you want the methods to be available to call by classes in other packages you should change the access modifiers to public.

Upvotes: 0

06needhamt
06needhamt

Reputation: 1605

the methods are mot visible because they are not public. Make them public and it should work Because methods without access modifiers are not visible to other packages even when you import the package

package animals;

public class dog {

    public void lick() {
        System.out.println("lick lick");
    }
    public void jump() {
        System.out.println("Whihoooo");
    }
}

Upvotes: 0

Dror Bereznitsky
Dror Bereznitsky

Reputation: 20376

The current visibility of both methods is package-private (no explicit modifier) which means they can be accessed only from classes within the same package. Since MainFile and dog are not in the same package, these methods cannot be accessed. You will need to declare the 2 methods as public.

Upvotes: 1

Shyam
Shyam

Reputation: 86

Default visiblity without any access modifier is invisible to the world.

http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Upvotes: 2

christopher
christopher

Reputation: 27336

Declare the methods as public.

public void lick()

Taken from this website

Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc. A variable or method declared without any access control modifier is available to any other class in the same package. The fields in an interface are implicitly public static final and the methods in an interface are by default public.

Where access modifier is the word public, private or protected before a variable or method declaration.


While I'm here, have a look at the Java Naming Conventions. These naming conventions are designed to make all code readable, and in a standard format.

Your first error is the naming of your dog class. The name should be Dog because all classes start with a capital letter.

Your second error is with your variable name Robbie. This should be robbie because all object references and variables should start with a lower case letter. If the variable has more than one word, it starts with a lower case letter, and all subsequent words start with an upper case value. For example validVariableName.

Upvotes: 0

Алексей
Алексей

Reputation: 1847

Because your methods lick and jump are package private i.e. the are only visible within package animal

to answer your question: make them either public methods or if package private is desirable move MainFile class into animal package

Upvotes: 0

Related Questions