File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Help with access modifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Help with access modifiers" Watch "Help with access modifiers" New topic
Author

Help with access modifiers

Gihan Madushanka
Ranch Hand

Joined: Apr 09, 2013
Posts: 78

Hello I am a beginner in Java and this is my first post in this forum.

Here is my problem

class loopy{
public static void main(String args[]){
System.out.println("not in the loop");
for(int x=1;x<=5;x++){
System.out.println("Still in the loop");
System.out.println("x ="+x);
}



}


}

I can compile the above program successfully.
It is supposed to print the value of x until x<=5.
But when I change the visibility of my class loopy to public, ie public class loopy it gives a compilation error.
I am grateful to anyone who could give me an answer.
Raghav Viswanathan
Greenhorn

Joined: Apr 26, 2012
Posts: 26

Hello Gihan,

Welcome to the java ranch.


Does work fine for me though. Not really sure as to what impact public access modifier on class would have on the result or output



Output is :
not in the loop
Still in the loop
x =1
Still in the loop
x =2
Still in the loop
x =3
Still in the loop
x =4
Still in the loop
x =5

Do reply with details about the error that you got. There should not be any compilation error as you say.

Thanks and regards,
Raghav.V




Better late than never.
Gihan Madushanka
Ranch Hand

Joined: Apr 09, 2013
Posts: 78
Hello Raghav Viswanathan !!!
Thank you for your prompt response
The error that I get is "class loopy is public should be declared in a file named loopy.java"
Then I changed my file name to loopy.java, earlier it was something else.
Now it compiled successfully irrespective of the access modifier of the class.(ie worked fine for both public and default)
Should class name and file name be identical?
Raghav Viswanathan
Greenhorn

Joined: Apr 26, 2012
Posts: 26

Please do use a good Hava reference book or Sun/Oracle tutorial on Java. This should give you a great head start. Most of the concepts are covered there and they are arranged in a sequence so that one can easily understand.

Should class name and file name be identical?


Yes they must be. Java is case sensitive too. Rule Number 1.

Please use th below URL

Java basics

Regards,
Raghav.V
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

Hi Gihan.

Public classes have to be in a file with the same name as the class. It's generally a good idea to do it for all classes (though sometimes it's convenient to have more than one non-public class in a file), but for public classes it's compulsory.
Gihan Madushanka
Ranch Hand

Joined: Apr 09, 2013
Posts: 78
Thanks guys
Yes I am going through head first Java book.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with access modifiers