File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with access modifiers

 
Gihan Madushanka
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 26
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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



 
Gihan Madushanka
Ranch Hand
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
Chrome Eclipse IDE Java
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys
Yes I am going through head first Java book.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic