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.

Similar Messages

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

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

  • 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

  • Access Modifiers Effect on Static Method Hiding Question

    I am studying for my SCJP exam and have come across a question that I do not understand and have not yet found a satisfactory explanation.
    Given:
    package staticExtend;
    public class A{
         private String runNow(){
              return "High";
         static class B extends A{
              public String runNow(){
                   return "Low";
         public static void main(String args[]){
              A[] a=new B[]{new B(),new C()};
              for(A aa:a)
                   System.out.print(aa.runNow()+" ");
    class C extends A.B{
         public String runNow(){
              return "Out";
    }The output is "High High". The explanation in the sample exam from ExamLab was that because the runNow() method in A was private that only an explicit cast to the B class would invoke the runNow() method in B. I have verified that that is the case, but am not clear on how the runNow() method being declared static in B and how the private access modifier in A results in this behaviour.
    Any additional explanation would be greatly appreciated.
    Thanks in advance.
    -- Ryan

    Ryan_Chapin wrote:
    OK, so since runNow() in A is private the compiler determines that regardless of the available methods in any of it's sub classes that since we declared the original array reference as "A" that it will invoke the runNow() in A. It's also due to the fact that the invocation came from within A. You would have gotten a compile time error if you tried to place the code in the main method in another class.
    >
    My mistake about the second part that you mention. You are correct. runNow() in B is NOT static, but the class is static. I guess that was the red herring in this question I don't see how that is related. I actually think that the "red herring" was what I described above. The fact that the code was placed in A, and that private methods can't be overridden.
    and the fact that the class itself is static has nothing to do with the behaviour that is being illustrated. Is that correct?Correct

  • To access a package, the calling class must reside in the root/parent direc

    My goal was to be able to access my package from anywhere within the
    high level root folder (d:\zJava). But it seems the invoking class MUST
    RESIDE IN THE TOP LEVEL PARENT DIRECTORY of the package. Was Java
    designed to behave this way?
    D:\zJava\zsamples\com\zswingpackage1>     <-- packages successfully compiled here
         swing_optgrp_eg1.{class/java}
              package com.zswingpackage1;
              public class swing_optgrp_eg1 extends JFrame implements ItemListener {
    D:\zJava\zsamples\com> <-- classes residing here could NOT successfully                    access the package 'com.zswingpackage1'. The third                    level subfolder is 'com'.
         package_test4.java
              /* Following import stmt. causing RUN-TIME error "Exception in thread     "main" java.lang.NoClassDefFoundError: com/zswingpackage1/swing_optgrp_eg1". */
              import com.zswingpackage1;
         Package_Test4.class          
         /* Unsuccessful remedies:
         i) D:\zJava\zsamples\com>java Package_Test4
                   -classpath .;d:\zJava\zsamples\com\zswingpackage1
                   -classpath .;d:\zJava\zsamples\com
                   -classpath .;d:\zJava\zsamples (also used in compile)
                   -classpath .;d:\zJava
         ii) ran from the root/parent directory of the package d:\zJava\zsamples),with and without classpath i.e.
              D:\zJava\zsamples>java com\Package_Test4 [-classpath .;d:\zJava\zsamples]
         iii) ran from the top level directory (d:\zJava), with and without classpath i.e.
              D:\zJava>java zsamples\com\Package_Test4 [-classpath .;d:\zJava\zsamples]
         Possible causes:
              - maybe the above behaviours are by design i.e. the calling class/program CANNOT be stored within the package hierarchy?
    D:\zJava\zsamples\net> <-- classes residing here could NOT successfully access the package 'com.zswingpackage1'. The third level subfolder is 'net'.
         package-test6.java
              /* Following import stmt. causing RUN-TIME error "Exception in thread "main" java.lang.NoClassDefFoundError: com/zswingpackage1/swing_optgrp_eg1". */
              import com.zswingpackage1;
         Package_Test6.class
         /* Unsuccessful remedies:
         i) D:\zJava\zsamples\net>java Package_Test6
                   -classpath .;d:\zJava\zsamples\com\zswingpackage1
                   -classpath .;d:\zJava\zsamples\com
                   -classpath .;d:\zJava\zsamples (also used in compile)
                   -classpath .;d:\zJava
         ii) ran from the root/parent directory of the package (d:\zJava\zsamples), with and without classpath i.e.
              D:\zJava\zsamples>java net\Package_Test6 [-classpath .;d:\zJava\zsamples]
         iii) ran from the top level directory (d:\zJava), with and without classpath i.e.
              D:\zJava>java zsamples\net\Package_Test6 [-classpath .;d:\zJava\zsamples]
         Possible causes:
              - maybe the above behaviours are by design i.e. the calling class/program MUST be stored in the parent folder of the package hierarchy?
    **D:\zJava\zsamples>     <-- classes residing here SUCCESSFULLY accessed package 'com.zswingpackage1'. NO -classpath parms. were necessary!
    D:\zJava\ <-- classes residing here could NOT successfully access the package 'com.zswingpackage1'. Similar scenarios as above.
    D:\zJavaB\ <-- classes residing here could NOT successfully access the package 'com.zswingpackage1'. I purposely created another folder with the 'B' suffix. Similar scenarios as above.
    My PC configuration:
         java version "1.4.0_01"
         Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0_01-b03)
         Java HotSpot(TM) Client VM (build 1.4.0_01-b03, mixed mode)
         JRun 4 (Build 47304)
         WinNT 4.0 SP5
         IE 5.50
         CLASSPATH=.;D:\jakarta-tomcat-3.3.1\lib\common\servlet.jar;
              C:\JRun4\servers\default\default-ear\default-war\WEB-INF\classes
         JAVA_HOME=C:\j2sdk1.4.0_01
         Path=C:\j2sdk1.4.0_01\bin;C:\WINNT\system32;C:\WINNT;
              C:\Program Files\Network Associates\PGPNT;C:\Program Files\MTS;d:\MSSQL7\BINN
    Thank you so much for your expertise.

    Hello,
    I think that you use the import-declarations incorrectly.
    Your code says:
    import com.zswingpackage1;
    ... The meaning of the above declaration is the following: Import the class "zswingpackage1" from the package "com". The compiler cannot find that class, but it is probably never asked to find it in your code, so it won't report an error.
    If you want to import the whole package use the following declaration instead:
    import com.zswingpackage1.*;
    ... That means: import all classes from from the package "com.zswingpackage1".
    You can also import just the classes needed, e.g.:
    import com.zswingpackage1.swing_optgrp_eg1;
    ... I hope that does the job.
    S&oslash;ren

  • Accessing database package variables from Forms

    I have a database package that populates a table of values, i.e.
    type t_route_list is table of rt_route.RTR_ID%type;
    route_list t_route_list
    route_list gets populated by a package function and I want to access route_list in the Form.
    While I can access other package variables by writing a function that returns the variable, Forms doesnt seem to like functions that return a user defined datatype as above. I have also tried using a procedure with an OUT param instead of a function but it also fails with same error.
    i.e.
    declare
    v_route_list pkg_bulk_route_replace.t_route_list;
    begin
    pkg_bulk_route_replace.init;
    pkg_bulk_route_replace.get_route_list(v_route_list);
    message(v_route_list(1));
    end;
    This will not compile, but removing the index (1) from the last line makes it compile but crash with ORA-0600.
    The code above with message replaced with dbms_out.put_line works fine on TOAD.
    So my question is......
    Can my database package return a plsql table to a form and if so, how?!

    Actually I've got this to work now!
    Thde main culprit appears to be a difference in the version of sql or pl/sql used on forms (version 5) and the database (8.1.7).
    I had defined my table as a nested table. By redefining this as a indexed table, simply by adding on 'index by binary_integer' on my server package, I am suddenly able to access the elements of the table on my form. Fortunately this did not break the server code either - the table was populated using bulk collect and that still works.
    Still got a bit of coding to do, but things are looking up now.

  • Multiple top level package declarations

    The "Programming Adobe ActionScript 3.0" states in chapter 4 "ActionScript Language and Syntax", "Packages and Namespaces", "Creating Packages" that you can declare at the top level of a package multiple variables, functions, and namespaces in addition to a single class as long as only one is declared "public".
    However, in Flash when I declare a public class and any other variable or function either with the "internal" attribute or no attribute, I get this error:
    5006: An ActionScript file can not have more than one externally visible definition: test.function1, test.Test
    The package code is as follows:
    package test
        internal function function1():String
            return "Function1()";
        public class Test
    The same thing happens if I replace the function with an internal variable declaration. According to the manual, any declaration with the "internal" attribute should not be externally visible outside the package. Only the "public" class declaration should be externally visible.
    Can anyone clue me in as to why I get this error?

    That is not the situation described by the quoted manual section that I am trying to recreate.
    It clearly says:
    "In ActionScript 3.0, you use the package statement to declare a package, which means that you can also declare variables, functions, and namespaces at the top level of a package. You can even include executable statements at the top level of a package. If you do declare variables, functions, or namespaces at the top level of a package, the only attributes available at that level are public and internal, and only one package-level declaration per file can use the public attribute, whether that declaration is a class, variable, function, or namespace."
    The data properties and class you have decleared are outside the package within the ActionScript file, and not at the top level of a package. The "public" attribute is not available outside the package at all.
    What I wish to know is why the quoted internal declarations at the top level of the package generate the quoted error.
    However, I am beginning to believe that the documentation is in error and that what it is actually describing IS the situation you just described. When the manual says "top level of the package", it really means "top level of the ActionScript file outside the package", and when it says "the only attributes available at that level are public and internal", it really mans "the only attribute available outside the package declaration is internal. At the top level of the package, only one declaration may be made, it must have the same identifier as the ActionScript file name, and it can have either the public or internal attribute. Code within the same file but outside the package declaration can not access an internal declaration in the package declaration."
    Actually, the whole paragraph would need to be re-written to clarify the issue and to unambiguously distinguish between "top level of a package" and "top level of an ActionScript file outside the package declaration".
    As a concrete example - two ActionScript files:
    MCTest.as is saved in the same directory as MCTest.fla, and the document class of MCTest.fla is set to "MCTest".
    package
        trace("MCTest package code.");
        import flash.display.MovieClip;
        import test.Test;
        public class MCTest
        extends MovieClip
            trace("MCTest class code.");
            function MCTest()
                trace("Created Class MCTest: " + Test.StaticMessage);
    trace("MCTest outside package code");
    Test.as is saved in a sub-directory called "test".
    package test
        trace("test.Test package code");
        public class Test
            public static const StaticMessage:String = "Test: Hello World!";
            trace("test.Test class code");
    var myField:String = "myField";
    function myFunction():String
        return "myFunction";
    trace("test.Test outside package: " + myField + ", " + myFunction() + ", " + test.Test.StaticMessage);
    The resultant trace output is:
    MCTest class code.
    MCTest package code.
    MCTest outside package code
    test.Test class code
    test.Test package code
    test.Test outside package: myField, myFunction, Test: Hello World!
    Created Class MCTest: Test: Hello World!
    It is interesting to note that the package and outside package code are executed AFTER the class code.
    That seems to make more sense. You can only declare one class, variable, function, or namespace at the top level of a package with the same identifier as the file name, public or internal, and you can include executable code. At the top level of the ActionScript file outside the package declaration, you can only declare internal classes, variables, functions, and namespaces, and you can include executable code, none of which are within the package nor have access to any package internal declarations.
    The problem, therefore, would seem to be an incorrect manual. Does anyone actually know if this is accurate and the intended behavior?

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

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

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

  • 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 ignored !?!

    Hi
    please, have a look at the code below and let me know your opinions
    // ------------- source file 1 ------------------------
    package pckg1;
    public class Dog {
         final public static void main (String[] args) {
              pckg2.Puppy puppy = new pckg2.Puppy("Spot");
              System.out.println(puppy.getName());
    // ------------- source file 2 --------------------
    package pckg2;
    class Puppy {
         String name;
         public Puppy(String name) {
              this.name = name;
         String getName() {return name;}
    Each class belongs to a different package. Obviously if you compile Puppy first and attempt compiling Dog the compiler will complain about several things (class Puppy not being public, method getName() not being public, etc...).
    Now,
    if you change the Puppy class so that the necessary bits are public, re-compile Puppy and then Dog it will work.
    When you subsequently go back to Puppy and remove the added public access modifiers and re-compile JUST the Puppy class and try running Dog IT WILL STILL WORK AS IF EVERYTHING IN THE PUPPY CLASS WERE PUBLIC.
    I don't thing this is right. If the access modifiers cannot be relied on then the whole system is flawed.
    PS: Running on W2K, the behaviour observed on JRE 1.2.2_007, 1.3.0_02 as well as on 1.4.0-beta-b65.
    Cheers
    Ales Krestan

    Thanks for the response, but I hold different views on the matter. Although performance surely is of a great importance, it should not come at the expense of consistency.
    A few more thoughts on the same:
    1/ my understanding is that it is responsibility of the class loader to decide what to do in such cases during the resolution case. Apparently the application class loader of SUN's JVM ignores it and so does the extension class loader of the same JVM.
    2/ surprisingly (or rather expectedly) when attempting to load both classes (with the dodged version of Puppy) using the system class loader the JVM throws java.lang.IllegalAccessError.
    3) the same trick cannot be done with an interface. If you try to dodge an interface using the same principle and try to load it using the application class loader JVM will refuse to run (or load) the class that uses the dodged interface.
    4/ the class loader DOES other things, so why not to check the access modifiers as well. It should check if the class can be instantiated, for example. See the section 2.17.3 Linking: Verification, Preparation, and Resolution of the JVM specification.

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

Maybe you are looking for

  • Access to MySql Database in the Web Service

    Hi all, In my j2me web service project, i want to access to MySql database in the web service and my installation is Tomcat 5.0 for Java WSDP, Java Web Services Developer Pack 1.5, MySql Server 5.0 and environment variables are adjusted... server.xml

  • How can I get the MAC address of my Apple TV?

    How can I get the MAC address of my Apple TV? I currently do not have an HDMI device to connect it to, and the MAC is not listed on the box or the Apple TV itself. (to answer the obvious, I am trying to pair it with an iPad, and I need to register th

  • Is there a way to create a web journal based on faces?

    I have a bunch of family photos from all branches of my family tree. I would like to create a web journal has photos of each individual on their own page. Does Aperture do that?

  • Adobe After Effects CS5 Crash and compatibility problems with Windows 7 64-bit

    Hello, I get a lot of crashes of After Effects CS5. I get the message thar Monitor Kernel program stopped responding and recovered. I also did a compatibility test and found that After Effects CS5 is not compatible with Windows 7 Premium 64-bit. Coul

  • PreparedStatement.executeBatch() Not Working

    Hello, I'm having a problem with the executeBatch() command. It seems that whenever I used it, my program just hangs up. What this program do is that it first read a text file and place it in an Array. Then it would also read the Database and place t