Jump to content
Sign in to follow this  
Debis

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
Typo

Share this post


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.

Share this post


Link to post
Share on other sites

Can you please elaborate with a small example of 2 classes and one main method? I just want to check the usage of curly braces of classes with respect to the main class.

Share this 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.

Project.class

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

Example.class

public class Example {
}

Share this post


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.

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

Loading...
Sign in to follow this  

×
×
  • Create New...