• Register
1 vote
20 views

Problem:

I have a very basic question being a novice. It might be silly to you but I really want to know the fact.

Let’s say you have a java program. Can you have two classes in one java file?

What is the reason behind it? Thanks for your clearance.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote

Solution:

Yes, we can. All we have to do is make sure that only one of them is public. A java file can’t run without a public class.

The below example will help you to understand it well. If you still have any questions, just reply to this thread.

public class MyTestClass {
  int a = 5;
}
class MyOtherClass {
  public static void main(String[] args) {
    MyTestClass myOb = new MyTestClass();
    System.out.println(myOb.a);
  }
}

Thanks.

13 9 6
94,260 points

Related questions

0 votes
1 answer 5 views
5 views
Problem: Can someone guide me. What is the purpose of having more than one class in a Java file? I am new to Java.
asked Mar 19 ummesalma 29.2k points
0 votes
1 answer 243 views
243 views
Problem: Hi there! I have been learning to program for a long time now and I recently learned inheritance in Java. I wrote the following program for practice: class Institutions { public void print_institutions() { System.out.println("This is an institution"); } } class ... , I have no idea what is causing this error. Can anybody here please clarify the cause of the error and its solution? Thanks!
asked Dec 29, 2020 Code Learner 9.8k points
0 votes
1 answer 6 views
6 views
Problem: I have got multiple classes which each implement multiple different methods within each. Now the problem statement is that I wish to use the methods from all these (maybe around ~200 such different class files/methods) in another class file which all ... can use multiple classes, without the overhead of declaring all the methods in each class that is being referenced inside the Interface?
asked Apr 24 sumaiya simi 43.9k points
0 votes
1 answer 3 views
3 views
Problem? I have multiple web-apps running on an app server and each web-app WAR file contains a copy of the same jar file. Does this mean that a class in that jar file will be loaded multiple times in the JVM?
asked Apr 12 anika11 32k points
0 votes
1 answer 13 views
13 views
Problem Hey, I just want to know what are nested classes?
asked Dec 19, 2020 chris jordan 2.4k points
0 votes
1 answer 23 views
23 views
I have created a Person class with two attributes, age and name, and later I have implemented a vector of people to try to print the attributes of the objects entered there. When I try to access the age attribute, there is no problem and it prints the values to me, but when trying to access ... people_vector.begin ();   while (it! = vector_people.end ())   {     it-> getName ();     it ++;   } }
asked Aug 15, 2020 game 4.6k points
0 votes
1 answer 20 views
20 views
What is the purpose of having more than one class in a Java file ? I am new to Java.
asked Dec 30, 2020 TeamScript 13.5k points
0 votes
1 answer 11 views
11 views
Problem: As I previously stated in my question, I am having difficulty learning. Could someone help assist me in completing my project?
asked Apr 1 rakib1 51.5k points
1 vote
1 answer 3 views
3 views
Problem: Can someone please help fix this uncaught error : c# multiple classes in one file
asked Apr 7 MUHAMMAD MUNEEB 83k points
0 votes
1 answer 12 views