Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Package Statement.

 
Suresh Rajadurai
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi folks.

I have a program in the folder c:\javatest\p




I have compiled this program and it created a class file in the same folder.

I created another program in the folder c:\javatest




The second program gives compilation error:

cannot find symbol
symbol: variable i
location class parent.



Then I replaced the i with public variable j, assuming because it was declared protected it gave compilation error, but still for variable j, even though it was declared as public it gives the same compilation error. can anybody throw some light on it please.


much appreciated


thanks in advance


Suresh.
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To access i variable this line is wrong.


Since the child class is in different package , you extend parent to inherit it.
you dont need to create parent object.
use the following line:


it will work..
 
J radolf
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
protected variables cannot be accessed through parent objects outside package.

so line 7 is wrong by java rules.
check it

but in case of public variable J it can be accessed check it again.
 
Manish R Singh
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please refer a similar post by Ashutosh chauhan Package problem
http://www.coderanch.com/t/435718/Java-General-beginner/java/Package

He had a excellent query(accessing outer package from inner package) after several attempts he got the answer for that!

you have to just include the java -cp <path> <filename.java> command to compile
. for current directory
.. for parent directory

you can use these relative naming pattern for setting appropriate path


Also have you placed the second file in a package or not .i suppose you have not !
Also most people do not know protected fields are accessible only through inheritance,you cannot make a reference and access a protected field i suppose this is not the major cause as you were saying it gives same error when field is changed to public !so it got to be the classpath problem please read the above and if you have time read the Package Problem from Ashutosh Chauhan!
 
Manish R Singh
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[MG] Edited. Please use the forum not the mail.
http://faq.javaranch.com/java/UseTheForumNotEmail
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hagemaru Shou wrote:

Please check your private messages for an important administrative matter.

Also please read http://faq.javaranch.com/java/UseTheForumNotEmail
 
Manish R Singh
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have not provided my email Godbole
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manish Chodle wrote:


Not good.
Please check your private messages again.
 
Manish R Singh
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now i have posted my original name i can give you proof for my name
 
Komal Amaresh
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you have extended the parent class, its variables can be accesed by the child class. the parent class is in a subdirectory p. so you cannot access it through the object of the parent class. try it by an object of the child class itself.

Komal
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manish Chodale wrote:Now i have posted my original name i can give you proof for my name

Account closed. please stick to the original topic.

/Dave
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic