0

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();
    }
}
1
  • 3
    Class name always starts with Upper Case Letter check Commented Aug 23, 2012 at 5:40

4 Answers 4

5

That is actually a constructor. You call it with new.

 create panel = new create();

Having a class with a lowercase name is highly unorthodox.

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

1 Comment

Thank you heaps!!! As you can see I'm quite new to java. I will accept your answer when it lets me in 10 min :) Cheers!
3

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.

Comments

2
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.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.