0

I am a beginner in Java. Could anyone please help me understand the following concept?

What I have done here is I tried to create a class as Sample, which I mentioned below, where I am printing You are in Main Method

class Sample 
{
    public static void main(String args[]) {
        System.out.println("You are in Main Method");
    }

}

and saved this java file as Student.java.

I didn't get any error in Eclipse.

Now, I had specified a public in front of this class as public class Sample and I am getting an error.

Could anyone please clarify for me with the right answer, as I am finding this to be difficult to understand?

lucian.pantelimon
  • 3,673
  • 4
  • 29
  • 46
  • you need delete ` symbols – alnasfire Feb 01 '13 at 11:36
  • 2
    I think you should do a little reading first about Classes and access level modifiers. http://docs.oracle.com/javase/tutorial/java/concepts/class.html http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html – Mythul Feb 01 '13 at 11:37

5 Answers5

4

In Java all classes with scope public must be save in file which name is exactly the same like name of this class. So if you have class named Sample it has to be saved in file named Sample.java. If class is named Student then file should be named Student.java

One of reason for this is that packages named and class names can be easly mapped to real system paths.

Michał Kupisiński
  • 3,765
  • 1
  • 19
  • 23
  • dude, could u explain this statement with some more example its not clear "One of reason for this is that packages named and class names can be easly mapped to real system paths" –  Feb 06 '13 at 09:10
0

Ensure that the class name is equal to the file name appended with ".java". So if you name your class Sample then the file should be named Sample.java. This is why you're getting the error because your class name is different to what you have named the file.

For future references, when you're getting a compiler error/run time error, whatever, please ensure that you list the error here. It makes it a lot easier to deal with the problem, and the chances that you will get an answer that actually solves your problem increases.

Force444
  • 3,321
  • 9
  • 39
  • 77
0

All public class must be saved with the same name. So if you have class named Sample it has to be saved in file named Sample.java

If you use a package, you have to save file in this folder. Example com.example, you have to save Sample.java in a folder root/com/example.

package com.example;

public class Sample{
   public static void main(String[] args){
   // put here your code
  }
}
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
0

Public classes must have their own compilation units (i.e. .java files which must match the class name) and are compiled into ClassName.class files.

This way the JDK knows where it should generate the output .class files and the JVM know where to load the .class files from.

An exception to this would be inner/nested classes which do not require a separate file and that get their byte code generated into files like OuterClassName#InnerClassName.class. Inner classes are stored in the same .java file as the outer class that defines them.

Note:ClassName, OuterClassName and InnerClassName are the names of the classes defined by you.

lucian.pantelimon
  • 3,673
  • 4
  • 29
  • 46
0

first u read visibility of modifies i think u r well understanding ur problem

Access Modifiers

Same Class  Same Package    Subclass    Other packages

public Y Y Y Y protected Y Y Y N no access modifier Y Y N N private Y N N N

maulik
  • 1
  • 1
  • could you explain in detail how this Access Modifiers is linked with my question above, i couldn't able to understand the solution for my above question if u could explain in detail i will be more thankful, thanks in advance –  Feb 03 '13 at 15:18