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?
In Java all classes with scope
publicmust be save in file which name is exactly the same like name of this class. So if you have class namedSampleit has to be saved in file namedSample.java. If class is namedStudentthen file should be namedStudent.javaOne of reason for this is that
packagesnamed andclassnames can be easly mapped to real system paths.