Reputation: 2641
I have this code but I am unsure how to call the public create()
method with the public static void main(String[] args)
method in a different class. I have searched the net but found nothing on just public methods just public void methods.
Here is the code
mainclass.java:
public class Mainclass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
create createObject = new create();
createObject.create();
System.out.println("dammit");
}
}
create.java
public class Create extends javax.swing.JPanel {
public create() {
initComponents();
setDate();
}
}
Upvotes: 0
Views: 5688
Reputation: 6783
Starting with something Off-topic but you might want to read about the Java Naming Convention to understand how to name your classes/methods/variables in Java.
Constructors as we know have declarations that look like method declarations—except that they use the name of the class and have no return type. So you can have something like this:
public class create extends javax.swing.JPanel {
/** Normal Constructor */
public create() {
}
/**
* Normal method for initialization.
*/
public void create(){
initComponents();
setDate();
}
}
I tend to prefer having a specific method to handle object initialization (or setup, if you may) for my classes i.e. kept all business logic separate from object creation mechanism. So, if I were you, I would've renamed my method as public void doCreate()
and initialized everything over there. (This is subjective and a matter of preference). With this case, my MainClass
would have changed something like this:
public class MainClass {
/**
* @param args
*/
public static void main(String[] args) {
create createObject = new create();
createObject.doCreate();
System.out.println("dammit");
}
}
Upvotes: 1
Reputation: 6158
public create() {
initComponents();
setDate();
}
create()
Is a constructor so when you say Create createObject = new Create();
the without parametrized constructor will automatically call.
read here for constructor
and please follow the java naming conventions class name always start with caps letter.
Upvotes: 2
Reputation: 62439
That is a constructor, not a normal method. You can see that it has the same name as the class it's in. It's called here:
create createObject = new create();
BTW, calling a class create
makes me a sad panda.
Upvotes: 3
Reputation: 262474
That is actually a constructor. You call it with new
.
create panel = new create();
Having a class with a lowercase name is highly unorthodox.
Upvotes: 5