简体   繁体   中英

How do you create an instance variable to call a non-static method from another class?

class V
{
    void print_V(int number)
    {
        String v= "v";
        int count= number;
        System.out.print("Letter" + v.repeat(count));
    }

    void print_V()
    {
        System.out.print("v");
    }

    public static void call_Print_V(int n)
    {
        print_V(n);
    }
 }

class Main 
{
    public static void main(String[] args) 
    {
        v call_Print_V = new v();
        call_Print_V.print_V(input);
    }
}

I tried searching on the Internet for how to create instance methods but found none of them helpful. I tried to format the instance method in a similar way to the examples that I found but I still couldn't get my method to be called.

Try to make the method you want to call public. I assume this is Java. So instead of:

void print_V()

do:

public void print_V()

Methods of classes are private by default. Private methods can not be called outside of their class scope. Also, I would consider not declaring public or private bad practise. Same goes for snake case, so please read the following article about code conventions. It makes live for other programmers a lot easier. https://www.geeksforgeeks.org/java-naming-conventions/

You are trying to call print_V in a non static context. Give your class a constructor and call the method from the constructor. Also, the recursive class initialzer call in main only needs to be

new V();

You can refer to your class in there as "this" (keyword) so no reference required if you make an ondestroy listener method for System.exit()

Also, input variable looks like it should be args[1] from main array argument to feed into constructor as argument

new V(new Integer(args[1]).intValue());

An instance variable is any non-static variable "created as a global at construction time". It usually is initialised in a constructor, or remains null until something wants to use it. Only globals, instance variables in the class, can remain null until they are required for use.

            import W;
            
            class V{
            W otherclass; // non static instance variable a global
            boolean startedW = false; // non static instance variable a global
        int arg1; // this classes non static  instance variable a global
            
            // a constructor is for when a class starts
            // constructor a constructor is NOT a method only a class initializer
            V(int arg1){ // any arguments fed to the class go in the ellipses curved brackets
        this.arg1 = arg1;
            otherclass = new W();
            startedW = true;
    // next line calls a method from class W
        T typo = otherclass.someMethodToCall();
    // would probably follow is more work here but...whatever
            }// end of constructor 1

// constructor overloading , a second constructor
V(String args1){
arg1 = new Integer(args1).intValue();
} // end constructor 2
  

public static void main(String args[]){
// doing anything much in main is difficult
// because of non static context errors for variables and methods
new V(args[1]);
}
  
  }// end class

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