• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java access modifier related question

 
shivika sharma
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am studying for OCAJP exam and following a stydy guide by Jeanne B & Scott S.

In Chapter 4 about Private Access Modifiers; in the book code says compiler error is on line 5 and 6:



But When I actually run the Program, it shows compiler error at line 4.

BadDuckling.java:4: error: cannot find symbol
                FatherDuck duck = new FatherDuck();
                ^
  symbol:   class FatherDuck
  location: class BadDuckling
BadDuckling.java:4: error: cannot find symbol
                FatherDuck duck = new FatherDuck();
                                      ^
  symbol:   class FatherDuck
  location: class BadDuckling
2 errors


Which one should be correct?
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Did you forget to import the FatherDuck class? Are you using an incorrectly classpath? Or didn't setup the package directories?

Please show us the FatherDuck code, your classpath (either environment variable, or command line), your file directory structure (along with location of the class files).

Henry
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the FatherDuck classBoth classes are in the same package pond.duck. Did you compile the FatherDuck class prior to compiling the BadDuckling class?

Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just have tried compiling both classes and I get the expected error messages as mentioned in the study guide.

Assume you have created two Java source code files FatherDuck.java and BadDuckling.java in the directory /home/java/testing/pond/duck. Then you should follow these steps to compile the source code files

Hope it helps!
Kind regards,
Roel
 
shivika sharma
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Roel..

I again compiled the way you mentioned. But still getting the same error.

E:\users\Personal_Folder\testing\pond\duck\BadDuckling.java:4: error: cannot find symbol
        FatherDuck duck = new FatherDuck();
        ^
  symbol:   class FatherDuck
  location: class BadDuckling
E:\users\Personal_Folder\testing\pond\duck\BadDuckling.java:4: error: cannot find symbol
        FatherDuck duck = new FatherDuck();
                              ^
  symbol:   class FatherDuck
  location: class BadDuckling
2 errors


I have attached snap shot of my directory structure.

I believe BadDuckling class is not able to read FatherDuck class.. but I don't understand why?

pond_d.png
[Thumbnail for pond_d.png]
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ss Sharma wrote:I believe BadDuckling class is not able to read FatherDuck class.. but I don't understand why?

Could you share the source code of the FatherDuck class?
 
shivika sharma
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here it is..

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ss Sharma wrote:here it is..

Nothing wrong with this class.

Can you share the javac commands you are trying to execute as well?
 
shivika sharma
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C:\Program Files\Java\jdk1.7.0_79\bin>
C:\Program Files\Java\jdk1.7.0_79\bin>
C:\Program Files\Java\jdk1.7.0_79\bin>javac E:\users\Personal_Folder\testing\pond\duck\FatherDuck.java

C:\Program Files\Java\jdk1.7.0_79\bin>javac E:\users\Personal_Folder\testing\pond\duck\BadDuckling.java
E:\users\Personal_Folder\testing\pond\duck\BadDuckling.java:4: error: cannot find symbol
        FatherDuck duck = new FatherDuck();
        ^
  symbol:   class FatherDuck
  location: class BadDuckling
E:\users\Personal_Folder\testing\pond\duck\BadDuckling.java:4: error: cannot find symbol
        FatherDuck duck = new FatherDuck();
                              ^
  symbol:   class FatherDuck
  location: class BadDuckling
2 errors
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ss Sharma wrote:C:\Program Files\Java\jdk1.7.0_79\bin>
C:\Program Files\Java\jdk1.7.0_79\bin>
C:\Program Files\Java\jdk1.7.0_79\bin>javac E:\users\Personal_Folder\testing\pond\duck\FatherDuck.java


The default classpath is ".", so, if you are going to compile from that directory, you will need your classpath set correctly.

What is your CLASSPATH environment variable set to?

Henry
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ss Sharma wrote:C:\Program Files\Java\jdk1.7.0_79\bin>javac E:\users\Personal_Folder\testing\pond\duck\BadDuckling.java

That's the reason why you get another error! You run the javac command from the installation directory of your JDK installation. So that means this directory is the current directory and by default the classpath is the current directory. But in this directory the javac command can't find the FatherDuck class because it resides in the directory E:\users\Personal_Folder\testing\ (and that's why you get another compiler error).

You have two alternatives to fix this issue:
  • set the current directory to E:\users\Personal_Folder\testing\ and run the javac commands (that's what I did in my previous post)
  • use the -cp option to define another classpath when you compile the BadDuckling class. This javac command should work: javac -cp E:\users\Personal_Folder\testing\ E:\users\Personal_Folder\testing\pond\duck\BadDuckling.java


  • Hope it helps!
    Kind regards,
    Roel

    PS. Please note that you are using JDK7 while you are preparing for the OCAJP8 certification exam. So you need to upgrade your JDK installation to JDK8 (otherwise you won't be able to compile code snippets using lambdas and the new date/time API).
     
    shivika sharma
    Greenhorn
    Posts: 11
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    yayy!!  it worked

    thanks a lot for your help..

    yes.. i have jdk1.8 installed at home where i practice most for certification..
    this is my office laptop which is on old version  
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ss Sharma wrote:yayy!!  it worked

    Glad to hear you got it working!

    In this topic you'll find a very detailed explanation about how to compile and run Java code (even with classes in different packages). Definitely worth reading!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic