-1

I would like to compile in a java source file with two classes. How can I do that? I compile my code with:

javac -classpath Class_ex_1.java 
public class Class_ex_1 { public static void main(String[] args) { int del = 7; int del_1 = 2; int rem = del % del_1; System.out.println("First value :" + del + "\n"); System.out.println("Second value :" + del_1 + "\n"); // System.out.println("Display meaning a % b :" + rem + "\n"); // Hello_test n1 = new Hello_test(); System.out.println("Display parameter from class:" + n1.getColor + "\n"); } } public class Hello_test { String color = "Red"; public String getColor(){ return color; } } 
7
  • The JLS allows only a single public class per file, and the class must have the same name as the file (FileName.java -> public Class FileName { ... }). Thus, the code presented is not valid java code and can therefore not be compiled. Commented Sep 17, 2020 at 22:31
  • 1
    Step 1: Remove public from classes that are not named the same as the file. --- Step 2: Remove -classpath from the command. Commented Sep 17, 2020 at 22:32
  • I did that, I got error : javac Class_ex_1.java Class_ex_1.java:13: error: cannot find symbol System.out.println("Display parameter from class:" + n1.getColor + "\n"); ^ symbol: variable getColor location: variable n1 of type Hello_test 1 error Commented Sep 17, 2020 at 22:51
  • You're invoking the method incorrectly. Change n1.getColor to n1.getColor() Commented Sep 17, 2020 at 23:07
  • 1
    Does this answer your question? Can a java file have more than one class? Commented Sep 18, 2020 at 9:44

2 Answers 2

1
class Hello_test{} 

Simply remove public from the second class. Also make sure the other class is inside of the Class_ex_1

Sign up to request clarification or add additional context in comments.

1 Comment

I did that, I got error : javac Class_ex_1.java Class_ex_1.java:13: error: cannot find symbol System.out.println("Display parameter from class:" + n1.getColor + "\n"); ^ symbol: variable getColor location: variable n1 of type Hello_test 1 error
1

The following code doesn't compile because it cannot find getColor

public class Class_ex_1 { public static void main(String[] args) { int del = 7; int del_1 = 2; int rem = del % del_1; System.out.println("First value :" + del + "\n"); System.out.println("Second value :" + del_1 + "\n"); // System.out.println("Display meaning a % b :" + rem + "\n"); // Hello_test n1 = new Hello_test(); System.out.println("Display parameter from class:" + n1.getColor + "\n"); } } class Hello_test { String color = "Red"; public String getColor(){ return color; } } 

So it should instead, look like the following:

class Class_ex_1 { public static void main(String[] args) { int del = 7; int del_1 = 2; int rem = del % del_1; System.out.println("First value :" + del + "\n"); System.out.println("Second value :" + del_1 + "\n"); // System.out.println("Display meaning a % b :" + rem + "\n"); // Hello_test n1 = new Hello_test(); System.out.println("Display parameter from class:" + n1.getColor() + "\n"); } } class Hello_test { String color = "Red"; public String getColor(){ return color; } } 

Remember that when we call a method, we need to have a set of parenthesis at the end.

5 Comments

You've answered a different question to the one asked in the Question.
@Scratte It was just more verbose answer, although slightly mutated.
@TheCreatingCoder Thank you very much, for comprehensive reply!!
@BilowYuriy No, it does not. It answers your other question, but doesn't at all address "How to compile several classes in one ,java file".
@Scratte Ok, you are right! I set right mark to other question

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.