-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;
    }           
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Bilow Yuriy
  • 1,239
  • 3
  • 13
  • 20
  • 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. – Turing85 Sep 17 '20 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. – Andreas Sep 17 '20 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 – Bilow Yuriy Sep 17 '20 at 22:51
  • You're invoking the method incorrectly. Change `n1.getColor` to `n1.getColor()` – WJS Sep 17 '20 at 23:07
  • Your Question seems to be mutating in the comment thread. The fact that you get a new error when you fix the original one is irrelevant to this Question. – Scratte Sep 18 '20 at 09:37
  • 1
    Does this answer your question? [Can a java file have more than one class?](https://stackoverflow.com/questions/968347/can-a-java-file-have-more-than-one-class) – Scratte Sep 18 '20 at 09:44
  • @Scratte You are right. The Question is mutated, but i thought it somehow relates to javac,But it turned out to be just wrong code. – Bilow Yuriy Sep 18 '20 at 10:00

2 Answers2

1
class Hello_test{}

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

Drew
  • 26
  • 3
  • 1
    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 – Bilow Yuriy Sep 17 '20 at 22:50
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.