• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Public Class not require

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please give the code snippet you have used.
 
Shankar sanjay
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You do know that in Windows you must use \ instead of / between parts of a path, don't you?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic