简体   繁体   中英

How to call a method from class inside another class

My main java code is like that:

package Javathesis;

//import... etc
//...


public class Javathesis; // My main class

{ 
    public static void // There are a lot of these classes here
    //...
    //...

    class x
    {
        String a;
        String b;

        x(String a, String b)
        {
            this.a = a;
            this.b = b;
        }
    }
    public void getAllDataDB1
    {
        ArrayList<ArrayList<x>> cells = new ArrayList<>();
        while(resTablesData1.next()) 
        {
            ArrayList<CellValue> row = new ArrayList<>();
            for (int k=0; k<colCount ; k++) {
            String colName = rsmd.getColumnName(i);
            Object o = resTablesData1.getObject(colName);
            row.add(new x(rsmd.getColumnType(),o.toString());
        }
        cells.add(row);
    }

    public static void main(String[] args) 
    {
        connectToDB1();
        // i want to call an instance of class "x" HERE!!!
    }
}

How can i call class x in public static void main? Am i doing something wrong? I already test the way i know

Class class = new Class();
class.x(String a, String b);

but i receive errors. Can somebody help me to fix this? Thanks in advance.

Since x is an inner class it need an outer class instance to obtain a reference:

Class x = new Javathesis().new x(a, b);

In addition the semi-colon needs to be removed from the class declaration

Methods in x can be invoked using the reference created

Java Naming conventions show that classes start with an uppercase letter. Also its good to give the class a more meaningful name than a single letter name

You should create an instance of your inner class

YourInnerClass inner = new YourOuterClass().new YourInnerClass(); 

and then call a method on it

inner.doMyStuff();

There are multiple problems with this code:

Class class = new Class();
class.x(String a, String b);

You cannot name a variable 'class', it is a reserved word. Also, x is a class - you cannot just call it, you need to instantiate it.

Also, why would you instantiate a Class - which is a class encapsulating knowledge about Java class?

Something like that might work:

Javathesis thesis = new Javathesis();
Javathesis.x thesis_x = new thesis.x("a","b);

Also, please, start class names with the capital letter - it is a convention in Java.

To call a class method without instantiating an object from that class, the method must be declared as static, and then invoked from wherever you want using the class name dot method parentheses arguments.

public class x {

//constructor, etc

public static int add(int x,y) {

return x+y;

}

}

//make the call from somewhere else

int y = x.add(4,5);

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