Debis Posted April 6, 2020 Share Posted April 6, 2020 (edited) 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 April 6, 2020 by Debis Typo Link to comment Share on other sites More sharing options...
Ingolme Posted April 6, 2020 Share Posted April 6, 2020 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 More sharing options...
Debis Posted April 7, 2020 Author Share Posted April 7, 2020 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. Link to comment Share on other sites More sharing options...
Ingolme Posted April 7, 2020 Share Posted April 7, 2020 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 More sharing options...
Debis Posted April 11, 2020 Author Share Posted April 11, 2020 Excellent. Classes in different files. That's where my doubt was. Thanks dear. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now