简体   繁体   中英

Accessing objects in other classes

I read in a java book that in order to access a method ect in one class, an object needs to be created using a constructor. Below is an example...

Class1
Class2 MyObject = new Class2();
MyObject.methodCall();

Class2
methodCall(){

}

Now I'm currently making an android app, and I used the above method, and I got errors, i then read a blog which gave an alternative way to access methods ect from other classes.

Class1
Class2 myObject;
myObject.myMethod();

Class2
myMethod(){

}

How does the above way work without the

new *InsertClassName*();

Edit

public class ButtonClickListener implements OnClickListener{
     VerificationMethods VM;
     VM.authenticateForFunctionsandNumbers();
}


public class VerificationMethods {
    ButtonClickListener BCL;
    public boolean authenticateForFunctionsandNumbers(){
         return trueOrFalse;
    }
}

Logcat:

05-25 17:19:02.195: E/AndroidRuntime(718): FATAL EXCEPTION: main
05-25 17:19:02.195: E/AndroidRuntime(718): java.lang.StackOverflowError
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonClickListener.java:41)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.VerificationMethods.<init>(VerificationMethods.java:5)
05-25 17:19:02.195: E/AndroidRuntime(718):  at com.example.myfirstcalculator.ButtonClickListener.<init>(ButtonCli
05-25 17:19:02.265: W/ActivityManager(61):   Force finishing activity com.example.myfirstcalculator/.MainActivity

Code:

ButtonClickListener.java

public class ButtonClickListener implements OnClickListener{
    EditText outputBoxID;
    String buttonID;
    Context context;
    static String valueofOutput;
    static double NumBeforeFunct;
    static double NumAfterFunct;
    static String rtAnswer;
    static char[] realTimeStringArray;
    static char[] valueofOutputArray;
    static StringBuilder valueofOutputBuild = new StringBuilder();
    static String NBFAS;
    static String NAFAS;
    static StringBuilder realTimeStringBuild = new StringBuilder();

    static String realTimeString;
    VerificationMethods VM = new VerificationMethods();
    //error occuring between here

    public ButtonClickListener(EditText outputBoxID, String buttonID, Context context){

        this.outputBoxID = outputBoxID;
        this.buttonID = buttonID;
        this.context = context; 

    }

    @Override
    public void onClick(View v) {

        valueofOutput = outputBoxID.getText().toString();

    ...

VerificationMethods.java

public class VerificationMethods {
    ButtonClickListener BCL = new ButtonClickListener(null, null, null);
    //error - ...and here

    public boolean authenticateForFunctionsandNumbers(){
        if(ButtonClickListener.valueofOutput.isEmpty()){
            return true;
        }else{
            return false;
        }

    ...

First, a quick explanation about the new keyword.

A class is like a blueprint to create objects. So in this code:

public class Cat {

    // This is a constructor
    public Cat() {

    }

    public void purr() {
        System.out.println("prrrrrrrr");
    }

}

The class is called Cat. Each time we write new Cat() , we are creating a new object (instance) of the Cat class. The constructor is called each time we create a new cat object.

Lets say we want the cat to purr. To do so, we would first need to create a cat object (otherwise, what is purring?), so we can write new Cat().purr(); .

This would create us a new cat, and then tell that cat to purr.

Let's say that there is a method that doesn't actually need a cat to work, for example:

public static void buyCatCollar() {

}

We could buy a cat collar without having a cat. We don't need an actual cat in order to do so.

We add the static keyword here, which means that the method belongs to the class , not to the object . This means that we can write Cat.buyCatCollar() , without having a cat object. This means we don't need to write new Cat() , because we don't actually need a cat to buy the collar.

Now moving on to your example.

When you write:

public class ButtonClickListener implements OnClickListener{
    VerificationMethods VM;
    VM.authenticateForFunctionsandNumbers();
}

It means that VM is an instance of the VerifcationMethods class. When you write VM.authenticateForFunctionsandNumbers(); you will get a null pointer exception, because you never actually set VM to a new instance. You would need to change it to:

public class ButtonClickListener implements OnClickListener{
    VerificationMethods VM = new VerificationMethods();
    VM.authenticateForFunctionsandNumbers();
}

OR, assuming you don't need a new instance, you could make the authenticateForFunctionsandNumbers() method static, and then write:

public class ButtonClickListener implements OnClickListener{
    VerificationMethods.authenticateForFunctionsandNumbers();
}

But this will not work unless you add the static keyword to your method.

My guess is in the second example you use static method, which does not require creating new object instances with new operator.

This will be usable anywhere, where you don't create object with new:

public class MyClass1 {
    public static void myMethod1(){
        //something something
    }
}

This requires new object created:

public class MyClass2 {
    public void myMethod2(){
         //something something again
    }
}

Now you can use:

MyClass1 myObject1;
myObject1.myMethod1();

But:

MyClass2 myObject2 = new MyClass();
myObject1.myMethod2();

You have to create a class (type), which is a definition for a later instantiated object of that type.

eg

public class MyClass {
    /**
     * Constructor method
     */
    public MyClass() {
        // some code
    }

    /**
     * Some method, that does something.
     */
    public void someMethod() {
        // do something here
    }
}

and later, somewhere else in your code you can get an instance of that type with using a call to that types constructor. eg

MyClass instance = new MyClass();

If you add more methods to the specified class, you can then call these methods on the instantiated object:

instance.someMethod();

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