Default/package/none access modifier

Hi,
I was hoping for some discussion on the default/package/none access modifier. It's always really bugged me that we have public, private, protected, and then "none", while it seems to me that it would be less confusing, and more consistent to use the keyword "package", or maybe even "default".
Then, source code would look like
public class MyClass{
    public int getValue() {}
    private void setValue() {}
    protected void someMethod() {}
    package int justForPackage() {}
}I know this concept has come up before, but the books I've read which mention this topic haven't offered any actaul justification/explanation for why there isn't some keyword.
Anyway, are there any insights as to why java is this way, and any reasons why java should or shouldn't be changed to include the package access modifier.

A good example is within a tightly grouped package (usually should be this way) you may have some cooperative classes that access methods.
// one .java file
public class SomeHelper {
   private void method() {
     new ClassForUsers().accessHiddenLogic();
// next .java file in same package
public ClassForUsers {
    /* default-access */ void accessHiddenLogic() {
}But, you may want to allow users to subclass your ClassForUsers, without giving them access to the hidden logic method directly:
// another .java file in a different package
public class UsersSubclass extends ClassForUsers {
    public void userMethod() {
        // can't do this
        accessHiddenLogic();
}This could be for either business logic or security reasons. So, package level access can be very useful. However, I've seen that in practice it is avoided because it isn't obvious what is going on.

Similar Messages

  • Non access modifier: default

    Top of the morning.
    Im studying for my SCJP exam and I have 2 text books.
    One of the text books asks the following question:
    (Im only writing the one answer im sceptical about)
    which of the following declarations are illegal?
    A. default String s;The answer at the back says that the above is illegal because defaultis not a keyword. But am I right in saying that unless you explicitly declare a class member
    as having one of the 4 access levels, it is automatically give default access - so in essence
    writing default is redundant if you don't give the member private, public or protected access.
    Nevertheless you could write default String s; couldn't you?
    The above declaration does compile.
    Thanks

    shamrock08 wrote:
    The answer at the back says that the above is illegal because defaultis not a keyword. [It is|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html], it is just not one [related access|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/switch.html].
    The above declaration does compile.It does not in Eclipse. Lets see your compiling code.
    Nevertheless you could write default String s; couldn't you?
    No.

  • Understanding the combination of inheritance, packages and access modifiers

    I am working on a problem to help my practical understanding of accessing inherited members and methods from a different package using both inheritance and instance variable.
    Unfortunately, I am totally lost when I tried to set this up and understand what was happening.
    Problem:
    Create a class named "classA" under package "pack1" with 4 members (int pub_a, int priv_a, int prot_a, int def_a) and methods (pub_func1, priv_func1, prot_func1, def_func1). Mark the members and methods with 4 different access specifiers (public, private, protected, and default)
    Create a class named "classB" under package "pack2", ClassB will inherit classA of package pack1. ClassB will have 2 members (int pub_b, int priv_b) and methods (pub_func2, prot_func2) with 2 different access specifiers (public, protected)
    Try accessing the members of class classA in package pack1 inside class classB of package pack2. Find out what members and methods are accessible and not-accessible through inheritance.
    Create an instance of class classA inside class classB of package pack2. Find out what members and methods are accessible and not-accessible through the instance.
    Note: In your solution, comment out the lines that are in-accessible with the actual error message at the top of the comment.
    So I produced the following code (I don't think i set this up right):
    //filename A.java
    package pack1;
    public class A {
         public int pub_a;
         private int priv_a;
         protected int prot_a;
         int def_a;
         public int pub_func1() {return 1;}
         private int priv_func1() {return 2;}
         protected int prot_func1() {return 3;}
         int def_func1() {return 4;}
    //filename B.java
    package pack2;
    import pack1.A;
    public class B extends A{
         public     int pub_b;
         private int priv_b;
         public int pub_func2() { return 5; }
         private int priv_func2() { return 6; }
         //directly inherited fields, at least one should be accessible?
         pub_a = 1;
         priv_a = 1;
         prot_a = 1;
         def_a = 1;
         int i;
         // directly inherited methods, at least one should be accessible?
         i = pub_func1();
         i = priv_func2();
         A a = new A();
         //not sure what's accessible here, as no error red underlines appear
         a.pub_a = 1;
         a.priv_a = 1;
         a.prot_a = 1;
         a.def_a = 1;     
    }I would be interested to know how one would approach and address this problem.

    //filename B.java
    package pack2;
    import pack1.A;
    public class B extends A{
         public     int pub_b;
         private int priv_b;
         public int pub_func2() { return 5; }
         private int priv_func2() { return 6; }
         //directly inherited fields, at least one should be accessible?
         pub_a = 1;//true
         priv_a = 1;// false
         prot_a = 1;//true
         def_a = 1;//false
         int i;
         // directly inherited methods, at least one should be accessible?
         i = pub_func1();//true
         i = priv_func2();//false
         A a = new A();
         //not sure what's accessible here, as no error red underlines appear
         a.pub_a = 1;//true
         a.priv_a = 1;//false
         a.prot_a = 1//false;
         a.def_a = 1;//false
    }Edited by: fun_with_me on May 31, 2008 8:30 AM

  • Access modifiers

    I dint understood the below question properly, please help me out.......
    You want subclasses in any package to have access to members of a superclass. Which is
    the most restrictive access that accomplishes this objective?
    A. public
    B. private
    C. protected
    D. transient
    E. default access
    I want to know whether the question is relating to class access modifiers or methods and variables of the classes...

    I didnt get you, please explain the topic properly.... I think classes can have only public and default as an access modifier, is it rite??? That's right. Look at [this,|http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html] it will give you a better understanding of the topic than you'd get from me answering your question.

  • What is the difference between access specifiers and access modifiers?

    what is the difference between access specifiers and access modifiers? are they same? if not what is the difference.

    Access Specifier are used to specifiy how the member variable ,methods or class to other classes.They are public ,private and protected.
    Access Modifier:
    1.Access
    2.Non Access
    Access:
    public ,private,protected and default.
    Non Access:
    abstract,final,native,static,synchronized,transient,volatile and strictfp

  • Default class access modifier

    What is the default access modifier for a class? I can't seem to find it in the tutorials...
    Thanks
    Jim

    The default access is the same for top level classes that do not specify access explicitly as for other identifiers. Package.
    This single source file will create two class files in the same package. Only other classes in the package can see these top level package access classes.
    And, yes, this is a bad idea. In the case of some build tools, errors in a compile can cause the public class to compile, but the package class to fail. After that, the compiler will not be able to determine what source file to compile for the package level class. This is an error that confuses many developers. I do not recommend this practice. Put each top level class in its own source file.
    � {�                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Usage of default access modifier

    Some programmers don't use the default access modifier (package level). Is there a specific reason? If so, what is it? Or is it a good to use default access modifier?
    Thank you,
    Srikanth

    Some prefer to grant or limit access to an object by the interface they expose. In this case all methods are either public or private. Access is restricted based on the fact that only the appropriate code is given references of certain interface types and if code does not have the right interface, they can not access the method.
    Its a different style, but I use it myself quite a bit. This way I never am concerned over if it should be public,private, protected or default. The choice is simpler. But sometimes it can make you create interfaces for really simple things which I am not uptight enough to do...

  • Default access modifier

    Hello,
    Can someone tell me what the default access modifier is in Java:
    -for methods
    -for member variables
    Thanks in advance,
    Balteo

    Friendly. Accessible to any other classes in the package.

  • Access Modifiers, Package declaration

    I created the following two classes in the same folder as two source files:
    A.java
    package abc;
    class A{
    B.java
    package abc;
    class B extends A{
    When I tried to compile, A compiled successfully. But when compiling B, it gives an error Cannot find symbol for class A.... I think it should be visible as both class access modifiers are (default). Why do I get this error????
    By the way if package declarations are removed from both classes then they compile successfully........

    i tried a lot..... does classpath affects compilation??? can u correct dis???If we're going to make the effort to help you, you could show willing and make the effort to spell out all your words and generally make your sentences as easy to understand as possible.
    Yes, the classpath affects compilation as the link provided shows. YOU can correct this.
    >
    e:\one\>javac -classpath e:\one\cert B.java
    >
    The classpath points at directories (or JAR files) not Java source files. Read the link.

  • Not  able to access default package classes in own package

    I created a class called Constant.java in default package(no package ).
    I have Connection.java in util package.
    I am trying to use the Constant.java in my Connection.java like below..
    public class Constant
         public static final String NEW = "01";
    package util;
    import Constant;
    public class Connection
    I am unable to compile the class Connection. My question here is Won't I able to access default package in my util package.. ?

    java_guy04 wrote:
    Djaunl , I understand.
    but, Will I able to access default package in my util package.. ? is this correct.No, you won't be able to use classes out of the "default" package.
    Because, AFAIK, if you use a classname that you haven't imported, Java automatically attempts to locate this class in the same package as the current class. It does not check the "default" package (unless that is also the current package), and since the "default" package does not have a package name, you can't import those classes either.

  • Access Class in Default Package

    Make long story short, I have to have some class in the "(default package)", related to JNI, and I will need to pass data back and forth from and to these classes from some packaged classes. I am using Java 1.4. As I know since Java 1.4, it's not possible to access classes in default package from packaged classes directly. Is there a work around? please help...

    I DON'T have the C/C++ codeSo you have to do with the "old" classes in the default package.
    Then use jschell's suggestion to devise proxying code that allow bridging between the packages.
    since Java 1.4, I can't call the java class in the default package from packaged class any moreIf I understand jschell's post, this restriction is enforced by the Java compiler , not by the Java interpreter (JVM). Indeed this is specified in the JLS (Java Language Specifications, http://java.sun.com/docs/books/jls/third_edition/html/packages.html#7.5), not the JVM specifications (I have not verified that latter point).
    So if you get an older JDK (before 1.4 they would accept the construct), you can try writing proxy classes within a named package, proxying to the old classes in the unnamed package. Once they are compiled, and jarred together for convenience, you can switch back to the current compiler (let's says 1.6) to compile the rest of your application classes, which only use the classes in the named package, using the jar as a library.
    When executing the program, the JVM will execute everything without complaining, since it supports execution of compiled code that has been compiled with former versions.

  • Doubt on 'default' access modifier

    How JRE access the main method even if the class has 'default' access.
    ie, following run without declaring the class as public.
    As per my understanding only classes in the same package access the method. Is the 'default' package belong to JRE ?
    class a {
    public static void main(String args[]){
    System.out.print("Welcome");
    }

    I am getting following error when I tried to run from command prompt.
    Exception in thread "main" java.lang.UnsupportedClassVersionError: HelloWorldApp
    (Unsupported major.minor version 50.0)
    at java.lang.ClassLoader.defineClass0(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)

  • Access Modifier: Only Subclass, not package wide?

    Hello fellow programmers
    I'm looking for an access modifier that allows access to a method or member only from a class and it's subclasses. So it must be weaker than private because subclasses have access and stronger than protected because I don't want it to be accessible package wide.
    Is there such a modifier in Java?
    Regards
    Der Hinterwaeldler

    There used to be in a very early version of the language but it was dropped for clarity. IIRC it was called "private protected." Now you have to settle for "protected."
    This makes kind of sense since you have more control over the classes in your own package than over the subclasses that are not necessarily written by you ... If you can't trust other classes in the same package maybe you should put the class in a new package...

  • Access default package

    Is it posible to access a class in default package from the some package called
    MyPackage
    if its not possible means why?
    thanx in advance

    why its not allowing to access?, is there practical
    problem to find the current folder while loading the
    classI guess it's to discourage from usage of the default package.
    You can't import the default package. It's not something that would be hard to implement. Classes in the default package were actually accessible in the first versions of Java.
    Kaj

  • Accessing default package in own package ****

    I created a class called Constant.java in default package(no package ).
    I have Connection.java in util package.
    I am trying to use the Constant.java in my Connection.java like below..
    public class Constant
    public static final String NEW = "01";
    package util;
    import Constant;
    public class Connection
    I am unable to compile the class Connection. My question here is Won't I able to access default package in my util package.. ?

    You indeed can't import from the default package.

Maybe you are looking for

  • Delta Initialization vs Delta Update

    We're having some issues with performance when loading our GL cube.  What I would like to do is perform many delta initializations, each delta inititialization containing one month/period. My confusion is in the fact that the last time I attempted th

  • JSP Best Practices and Oracle Report

    Hello, I am writing an application that obtains information from the user using a JSP/HTML form and then submitted to a database, the JSP page is setup using JSP Best Practices in which the SQL statments, database connectivity information, and most o

  • How do I sync two iPhones with my iTunes and still have different apps on each phone? Do I use home sharing?

    Does home sharing let me have the same iTunes library on mine and my girlfriends phones?

  • Compatibility Mode for itunes 10.3 due to error 2096

    i have uninstalled and reinstalled itunes several times and still get the "error 2096" message. followed all steps as directed for removal. does this have anything to do with the compatility mode? should it be checked or unchecked? can anyone help?

  • Swing applet view problem in IE

    Hi, I am new to Java and have to develop a web enabled tool where the servlets in my Solaris-Tomcat web server is manipulating with data in an oracle database. The client code is on Swing Applet, which is to be opened in Internet Explorer 5.5 from Wi