Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed how to create a new class in java?

+5 votes
asked Aug 1, 2023 by Saniya Fayaz (170 points)
closed Nov 27, 2023 by Admin
Other than class Main if I create another class, how can I run that program?
closed with the note: answered

4 Answers

0 votes
answered Aug 3, 2023 by Peter Minarik (86,640 points)
edited Aug 17, 2023 by Peter Minarik

Here is a simple example for you.

Main.java

public class Main
{
    public static void main(String[] args)
    {
        Person person = new Person("John Doe", 47);
        System.out.println(person.name + ", " + person.age);
    }
}

Person.java

public class Person
{
    public String name;
    public int age;
    
    public Person(String _name, int _age)
    {
        name = _name;
        age = _age;
    }
}

In Java, every class has to go to its own file (with the same name as the class itself).

To use code from your classes, you'll need to reference them from your main().

In the example above, the Person class is instantiated via the constructor. In the next line, we print out the fields held by the Person class.

One program can have only one entry point (main() function). If you want a different program, create a new project.

0 votes
answered Aug 9, 2023 by LiudmilaCh (140 points)
You can create just class without extension "public" and write all classes including main in one file .
0 votes
answered Aug 16, 2023 by Gulshan Negi (1,580 points)

Hello this is Gulshan Negi

Well, you can understand it with the help of below example:

public class MyClass {
    // Fields (variables)
    private int myField;

    // Constructor (optional)
    public MyClass(int initialValue) {
        myField = initialValue;
    }

    // Methods
    public void setMyField(int newValue) {
        myField = newValue;
    }

    public int getMyField() {
        return myField;
    }

    public static void main(String[] args) {
        // Create an instance of MyClass
        MyClass myInstance = new MyClass(42);

        // Call methods on the instance
        System.out.println("Initial value of myField: " + myInstance.getMyField());
        myInstance.setMyField(99);
        System.out.println("Updated value of myField: " + myInstance.getMyField());
    }
}

Thanks            

0 votes
answered Nov 10, 2023 by Aml (220 points)
for making class in java you can simply open java editor and write class and give a space and enter class name which do you want to give.here is the example,
class AML
{
    public static void main(String[] args)
    {
        System.out.println("I HOPE YOU UNDERSTAND WHAT AM I TRYING TO SAY");
    }
}
Here  i used class name AML you can take it by your prefrence but remember always class name never strat with number or any signs
i hope you understand
                                                                   ~AML
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...