• Post Reply Bookmark Topic Watch Topic
  • New Topic

Public Class not require  RSS feed

 
Shankar sanjay
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

A Class did not declare Public then i can run both Class in the same file..But in SCJP Cathy Seira Book (1.6)
mentioned (page number 11) that in a source file there should be one Public. If i didnt not declare any public i can access any class from command prompt.

Code :
-----
package com;

class Test {

public static void main(String[] args) {
// TODO Auto-generated method stub
int $x = 10;
System.out.println(/* printing x vlaue */$x);

}
}

class Test1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int $x = 20;
System.out.println(/* printing x vlaue */$x);

}
}

Output:
--------
D:\Eclipse\Workspace\Test1\src>javac com/Test1.java
error: cannot read: com/Test1.java
1 error

D:\Eclipse\Workspace\Test1\src>javac com/Test.java

D:\Eclipse\Workspace\Test1\src>java com/Test
10

D:\Eclipse\Workspace\Test1\src>java com/Test1
20
 
Anjali Raman
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please give the code snippet you have used.
 
Shankar sanjay
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have already given the complete code and the entire code place in Test.java file

Regards,
Sankar. S
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do know that in Windows you must use \ instead of / between parts of a path, don't you?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Rob said, try this:

javac com\Test1.java

Note: Use backslash \ and not forward slash / if you are doing this on Windows.
 
swapnl patil
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Java program is a collection of one or more classes, with one of them containing the program's execution starting point. A Java source
file can contain more than one class definition. The Java 2 SDK enforces the rule that at the most one class in the source file has public
accessibility
.
The name of the source file is comprised of the name of this public class with .java as extension. Each class definition in a
source file is compiled into a separate class file, containing Java byte code.

try this

class publicTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println("hello");

}

}
class publicTest1{

public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println("hello");

}


}

save file with name publicTest in eclipse & try to run . it will run fine.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!