0

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.

2

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

You should create an instance of your inner class

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

and then call a method on it

inner.doMyStuff(); 

Comments

0

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);

1 Comment

The problem here is an inner class. You barely answer the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.