• Register
1 vote
18 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,240 points

Related questions

0 votes
1 answer 16 views
16 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 5.5k points
0 votes
1 answer 9 views
9 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 9.9k points
0 votes
1 answer 2 views
2 views
Problem Hey, I just want to know what are nested classes?
asked Dec 19, 2020 chris jordan 2.2k points
0 votes
1 answer 1K views
1K views
Problem : Getting below eclipse error the class file viewer cannot handle the given input ('org.eclipse.ui.ide.filestoreeditorinput').
asked Nov 12, 2019 peterlaw 6.9k points
0 votes
1 answer 22 views
22 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 45 views
45 views
Problem : I have following two classes: JDialog and JFrame (see signature below). I want them to have few extra methods, but I also want them both to extend my code, without rewriting the code Is it possible to do that? java.awt.Window java.awt.Dialog javax.swing. ... the identical way. So I will not want to write the code twice. I want only one class to utilize the code of the other class.
asked Dec 7, 2019 alecxe 7.5k points
0 votes
1 answer 11 views
11 views
I want a program that will help me to copy one file to another file in Java.
asked Sep 22, 2020 Daniel Anderson 4k points
0 votes
1 answer 1.6K views
1.6K views
Problem : I am currently working on the project and i am getting the error as below : "Implicit super constructor MyPerson() is undefined. Must explicitly invoke another constructor" But I am unable to understand it. Below is my MyPerson class: public class ... and just giving it an instructor variable as below : public class MyStudent extends MyPerson { public MyStudent(String myInstructor) { } }
asked Dec 16, 2019 alecxe 7.5k points
0 votes
1 answer 22 views
22 views
Hi, when i try to execute this code i always get the error message from above, and i really want to know why, already tried to put a “public class XXXX” before the code, but not work anyway. public static String invertirPalabras(String cadena) { cadena += " "; String ... + palabra; if (cadena.charAt(i) == ' ') { nuevaCadena += palabra; palabra = ""; } } return nuevaCadena.trim(); }
asked Aug 2, 2020 Daniel Aguilar
3 votes
1 answer 1.4K views
1.4K views
Problem: Hello Kodlogs, I am a novice Java programmer. I just wrote my very first program a few minutes earlier. Now I want to write a program that can add two different integer numbers for me. I guess I done my coding well but it says something wrong with my writing and throws back an error: &ldquo ... []args){ int x = 10; int y = 15; int sum = x+y; System.out.println("Sum is: " + sum); } }
asked Mar 23, 2020 Gavin 15.3k points