Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    vermio is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default Java Class declaration

    Hi,
    I'm a very beginner in java and I don't want to have questions, that's why I'm asking:
    One class can be declared as static or private or nothing.
    If you don't declare it as public or private (eg. class myclass {...})
    what type is it?
    And why in an example program that I have, the compiler complains if I declare it as public or private but it's ok if I don't put anything in front of its name?:)

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Then the class have default access. Any class, field (variable), method or constructor that has no declared access modifier is accessible only by classes in the same package, not from the different packages. The default modifier is not used for fields and methods within an interface. If you can go to the Suns' tutorial, you can find a lot about those access modifiers.


  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    vermio is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    Thanks for the reply.

    I have the following simple program:
    class testClass {
    public static void main(String[] args) { int i = 1; }
    }

    I compile it and it's ok.
    If I make the class public or private I have (different) compilation errors.
    I can understand these kind of errors but it's obvious that java doesn't treat to the class neither as private nor as public.

    Eranga, what following page do you mean?

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    You shouldn't get any compilation errors for setting that class public.

  6. #6
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Hi vermio

    You can only have a single public class in a java source file.
    Java Code:
    public class Main {
    	public static void main(String[] args) {
    		int i = 1; 
    	}
    }
    Compiles fine.
    Java Code:
    public class Main {
    	public static void main(String[] args) {
    		int i = 1; 
    	}
    }
    
    public class Oops {
    
    }
    Doesn't compile and gives an error:
    Java Code:
    ...\Main.java:7: class Oops is public, should be declared in a [COLOR="RoyalBlue"]file named Oops.java[/COLOR]
    public class Oops {
           ^
    1 error
    If you have a different error, please post the error message.

    Thanks
    Tim
    Last edited by tim; 07-16-2010 at 09:50 AM.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  7. #7
    vermio is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    FileName: pr01.java

    Public class testClass {
    public static void main(String[] args) { int i = 1; }
    }
    pr01.java:1: class testclass is public, should be declared in a file named testClass.java

    Understood

    Private class testClass {
    public static void main(String[] args) { int i = 1; }
    }
    pr01.java:1: modifier private is not allowed here

    Understood

    class testClass {
    public static void main(String[] args) { int i = 1; }
    }
    No errors


    I think it's because it is considered as a package? Am I right?
    (I haven't studied about packages yet).

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    Well, it all stems from the filename being different.

    You can have as many non-public classes defined in a java file as you want. They will only be visible to other classes in that package. It is not to do with it considering the class a package.

    You can only have one public class defined (ignoring inner classes here), and no private classes.

  9. #9
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by vermio View Post
    FileName: pr01.java

    [I]Public class testClass
    [I]Private class testClass {
    public uses a small 'p' not a capital 'P' same goes for private....

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  11. #11
    Hema26 is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default

    if you done specify access modifier then class is package public means that class is accessible withing the package. where as public class is accessible outside the package also.
    i hope i have cleared ur doubt.
    Hema26

  12. #12
    Hema26 is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default

    hi
    if u declare the class public then u must save ur program with same name you have given to ur class. e.g
    if u have declared a class Student as public then u must save ur program with same name i.e. Student otherwise it will give error.
    regards
    Hema26

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    Txt spk is n abmntn.

  14. #14
    vermio is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    thank u all guys.

  15. #15
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  16. #16
    vermio is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    Eranga,
    sorry, but how can I do this?

  17. #17
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    On top of the thread you can see a menu. Click on thread tools, and on the drop down menu list you can find an option to mark the thread solved.

  18. #18
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    If a class doesn't have a modifier, what implicit modifier does it have?

    Java Code:
    //ERROR
    public class SomeStuff {
    
    }
    public class NotSomestuff {
    	
    }
    Java Code:
    //ERROR
    public class SomeStuff {
    
    }
    private class NotSomestuff {
    	
    }
    Java Code:
    //ERROR
    public class SomeStuff {
    
    }
    protected class NotSomestuff {
    	
    }
    Thus, the implicit modifier can't be public, private, or protected. What's left? abstract and final? Those are irrelevant in this case aren't they?

    The question is, what implicit modifier does a class have if no modifier is identified?
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    It's package protected (or whatever the offical term is).
    That is, only things in the same package can access it.
    I originally thought you could have protected, but you're right, that's not the case.

  20. #20
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    package private is the proper term :) package protected would infer that sub-packages can access it, and I'm quite sure that isn't the case.

    As for why your getting errors, your class is named testClass, and the file is pr01.java. The file should be testClass.java, or the class should be named pr01. (I think you got that, but just to be sure...)
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Page 1 of 2 12 LastLast

Similar Threads

  1. class Declaration
    By mahtab in forum New To Java
    Replies: 1
    Last Post: 11-01-2009, 06:49 AM
  2. Replies: 14
    Last Post: 05-28-2009, 05:44 AM
  3. global declaration in java
    By gpveena20 in forum New To Java
    Replies: 13
    Last Post: 02-12-2009, 10:25 PM
  4. whats wrong with this class declaration?
    By blossompark in forum New To Java
    Replies: 5
    Last Post: 11-30-2008, 02:18 AM
  5. Declaration
    By asifahmed in forum New To Java
    Replies: 1
    Last Post: 04-05-2008, 05:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •