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.

Similar Messages

  • 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.

  • 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.

  • 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

  • Access modifier for Constructors ???

    As constructors are not the so called Members of a class
    (Class's Member declarations include only 4 things : variables , methods, member classes and member interfaces)
    Why do we have access modifiers for constructors also ??
    I know that if a class A 's constructor is declared 'private' , then that class cannot be instantiated outside the class A. .That is, class A can be instantiated only inside class A and provide this newly created reference to the outside world through public getter method.
    I dont understand how the other access modifiers (protected and default ) apply to a constructor.
    Any help from ur side is greatly appreciated !!!!!

    Why do we have access modifiers for constructors also
    ??To prevent anyone from accessing them if they shouldn't.
    I know that if a class A 's constructor is declared
    'private' , then that class cannot be instantiated
    outside the class A. Or you use another c'tor. Or a static getInstance() method provided by A. You you simply shouldn't create an instance yourself anyway.
    That is, class A can be
    instantiated only inside class A and provide this
    newly created reference to the outside world through
    public getter method.Yepp.
    I dont understand how the other access modifiers
    (protected and default ) apply to a constructor. Same as at other places. No difference.

  • Private, protected Access Modifiers with a class

    Why cant we use private and protected access modifiers with a class?
    Thanks.

    Matiz wrote:
    >
    Public access allows you to extend a parent class in some other package. If you only want users to extend your class rather than instantiate it directly, make the class abstract and design for extension.Agreed. However, would the same argument be not true for the default access at the class level? No. Default access would only allow you to extend a parent class in the same package (as opposed to some other package).
    Now my confusion is why is a class allowed default access at the top level and not protected?Because protected for a top-level class makes no sense. The protected keyword provides member access to any other class in the same package and extending classes outside the package. A top-level class isn't a member of a class, by definition, so there's nothing that protected would do provide differently than public.
    So, the two access modifiers for a top-level class are public and default. Public allows access to the class outside the package, whereas default restricts access to the class within the package.
    ~

  • 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.

  • 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.

  • I want to uninstall and then reinstall Firefox but I can't uninstall it. Nothing happens when I click remove in program access and defaults and the helper.exe file in the firefox uninstall dir doesn't do anything either.

    I want to uninstall and then reinstall Firefox but I can't uninstall it. Nothing happens when I click remove in program access and defaults and the helper.exe file in the firefox uninstall dir doesn't do anything either.

    "program access and defaults" is not the place to remove programs in Windows. You need to go to the control panel and click on Add and Remove programs. For instructions on how to uninstall Firefox, see [[Uninstalling Firefox]]

  • Java class access modifiers

    Why java class cannot have private and protected access modifiers?

    class X {
      private class y {}
    }should compile just fine. A top-level private class makes no sense because you wouldn't be able to see it. As for protected, I don't know.

  • Grep style shortest match (non-greedy) modifier ignored

    I'm looking for confirmation on a possible bug that I've had problems with from InDesign version 5.5 up through the current CC 10.0.0.70.
    What I'm trying to do is apply a character style via a grep rule in a paragraph style that matches everything up until the first colon in a block of text. The goal is to make that text bold. My grep rule is
    .+?:
    which means match one or more of any character, shortest match (non-greedy in grep terminology), followed by a colon. But the non-greedy modifier is being ignored and it's matching all of the text up until the last colon.
    Here's a screen shot.
    I've come up with a work around, but it would be nice for it to work properly. I'll report it as a bug if others confirm having the same problem. Thanks.

    Thanks, Peter! I'm so used to the way grep works outside of InDesign that I forgot it would look for multiple instances within the paragraph. Technically, I did limit it to the first occurrence by using the question mark after the plus. (If you use any of the shortest match options in the drop-down menu that's what you get.)
    But anchoring it to the beginning of the line with ^ did the trick.

  • Use of public access modifier in main method

    I want to know what is the significance of public access modifier with main (String args[]) method. Like generally we write
    public static void main(String args[])
    But if we write
    private static void main(String args[])
    OR
    protected static void main(String args[])
    then also its working properly.............
    then what is the use of public keyword..........................
    Regards
    Ajay Pratap Singh

    then what is the use of public keyword..........................Convention, I believe. And I think newer versions of the JVM require it.
    P.S. Relax a bit on the punctuation overuse. Many folks around here find that a bit irritating, and you probably don't intend to send that kind of a message. Cheers!

  • Protected and public access modifier - exact difference

    Hi everybody,
    Please help me to give an exact difference between protected access modifier and public
    Protected: Classes with in the same package as well as subclasses can access.
    Public : Classes outside the current class can access while declaring public.
    My doubt is that what is the practical difference between public and protected.
    I searched the net , but the information was not satisfactory to solve my confusion.
    Thanks & regards
    Parvathy

    Obviously doubt = lacks the ability to think for
    oneself.When one person raises a doubt, that does n't mean that he lacks the ability to think . He/She might have thought about it and if he didnt get a proper clarification then it will be posted in the forum.
    In such cases please dont discourage others who are ready to answer the subject.Without knowing the actual intention , i request you please dont reply like this

Maybe you are looking for

  • How to change in Transfer structure

    Hi All, I want change the mapping of one of my info object in transfer structure. When I am clicking on the change-display button (pen/spec icon),it's not going into change mode & every thing remains disabled. I tried with SAPALL authorization from B

  • Using multiple XSL files parsing a XML file

    Hi, Can a single XML be procesed by a XSL file in which are included many XSL stylesheet? I mean, my Java servlet writes the results of the SQL queries in a single XML document. Something like this: <?xml version="1.0" encoding="UTF-8" ?> <body> <!--

  • (Spry) Horizontal Menu Alignment

    I am currently working on designing my first web pages. Ever. I am having trouble making my Spry horizontal menu reach to both sides of my page, which is I have constrained to 720px wide. I have followed a few of the suggested answers to other thread

  • PNP LDB

    Hi Abapers, I have used Standard PNP LDB in selection screen of my report, there is one button called 'org. structure' in standard Screen if u observe.. now my requirement is to create a new button 'SBU' just like the existing one.  by clicking on th

  • Trouble exporting comments to Word 2010

    I am trying to export comments that I've put in an Acrobat document [using Acrobat 9.4.2] to a Microsoft Word document.  I follow the procedure described in the help documentation to do this, but I keep getting an error message that says I need to ha