Jump to content

Confusion regarding CLASS in java

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
Link to post
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 post
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.


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


public class Example {
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...