Jump to content

Confusion regarding CLASS in java


Debis

Recommended Posts

I have 2 questions:

1. Is it the norm that a Java program can only have One class?

2. Is a Class in Java always"Public"?

If any of your answers is a No, can you please show a simple example program with System.out.println().

I'm a beginner, so, sorry if these questions appear stupid to you.

Edited by Debis
Typo
Link to comment
Share on other sites

Java programs almost always have many classes. If you don't have more than one class then you're not really taking advantage of Java's object-oriented nature and you might as well use a different programming language.

As a beginner, you should make all your classes public. There are cases where private or protected classes are better, but you need a lot of experience to identify them and the code for an example would be too long for a forum post.

Link to comment
Share on other sites

Each class has to be in its own file and you can use import statements to load the files. A Java IDE will usually handle this for you. You can read more about it in the W3schools tutorials.

Project.class

public class Project {
    public static void main(String[] args) {
        Example example = new Example();
    }
}

Example.class

public class Example {
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...