简体   繁体   中英

How can I invoke a method or access a field from other class in the same package in JAVA

I am a very beginner of JAVA programming. I am working on my course project which is creating a game. I created 2 classes for this game and they are in the same package. In class B, I need to invoke the method from class A, and access the field also. How should the code be to access the method which is defined in class A? I searched for the solution online and someone suggested that write the code like this:

Classname.methodname(args)

However, there is a compile error(I think)

error: cannot find symbol  public A newA;  symbol:   class A    location: class B

which the code I wrote is something like this:

newA.moveRight(A.getANumber());

One method I learnt from the lesson is that I extends the class to access the method. But those are 2 different class so I cannot extend the class.

Can someone tell me the reason about the mistake I made or the solution about this issue? Thank you so much for the help.

There are two ways to go about solving this problem. The first is make the methods static and second is make an instance of the other class.

Suppose you have to classes, ClassA and ClassB

ClassA

ClassA has no static methods and a public initializer, so the only way to call methods from ClassA is to make an instance of it.

public class ClassA{

    public ClassA(){
        //Intentionally blank
    }

    public void doSomething(int anInt){
        //Intentionally blank
    }

    public int doSomethingElse(){
        return -1;
    }
}

ClassB

ClassB has a private initializer and static methods, so it is non instantiatable and its methods can be called with out having an instance of the class.

public class ClassB{

    private ClassB(){
        //Intentionally blank
    }

    public static void doSomething(int anInt){
        //Intentionally blank
    }

    public static int doSomethingElse(){
        return -1;
    }
}

Now suppose you have this code:

public class ClassMain{

    public static void main(String... args){
        ClassB classB = new ClassB();//Will throw an error
        ClassA classA = new ClassA();//Will compile fine

        ClassB.doSomething(ClassB.doSomethingElse());//Will compile fine
        classB.doSomething(classB.doSomethingElse());//Will give a warning

        ClassA.doSomething(ClassA.doSomethingElse());//Will throw an error
        classA.doSomething(classA.doSomethingElse());//Will compile fine
    }
}

To call methods from ClassA you will need to define an instance( ClassA classA = new ClassA(); ) in order to call its methods. This is because the methods are not class methods.

To call methods from ClassB you can simply access them with the class name( ClassB.doSomethingElse(); ). The reason you can do this with ClassB is because its methods are static and are therefor class members.

Summary

A method must be static in order for its methods to be called by class name, otherwise you will need an instance of the class.

This is oracles tutorial about class instantation. This is oracles tutorial about class members.

I suggest you read through them in order to get a full understanding of inheritance, static members and so on.

Using the Java reflection can easily invoke the method and access the fields.

Class<?> clazzA = ClassA.class;
ClassA objectA = new ClassA();
Method method = clazzA.getMethod(methodName, parasType);
method.setAccessible(true);
method.invoke(objectA,paras);
Field field = clazzA.getField(fieldName);
field.setAccessible(true);
Object fieldType = field.get(objectA);

you can't just access a classes filed directly. so you need to create a setter and getter method to access or modify other classes fields

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM