File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Package Statement. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Package Statement." Watch "Package Statement." New topic
Author

Package Statement.

Suresh Rajadurai
Ranch Hand

Joined: Feb 22, 2007
Posts: 58
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

Joined: Apr 15, 2008
Posts: 131
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..


<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
J radolf
Ranch Hand

Joined: Jan 08, 2009
Posts: 121
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

Joined: Mar 14, 2009
Posts: 14
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

Joined: Mar 14, 2009
Posts: 14
[MG] Edited. Please use the forum not the mail.
http://faq.javaranch.com/java/UseTheForumNotEmail
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Hagemaru Shou wrote:

Please check your private messages for an important administrative matter.

Also please read http://faq.javaranch.com/java/UseTheForumNotEmail


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Manish R Singh
Greenhorn

Joined: Mar 14, 2009
Posts: 14
I have not provided my email Godbole
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 9990
    
    7

Manish Chodle wrote:


Not good.
Please check your private messages again.
Manish R Singh
Greenhorn

Joined: Mar 14, 2009
Posts: 14
Now i have posted my original name i can give you proof for my name
Komal Amaresh
Ranch Hand

Joined: Oct 06, 2008
Posts: 67
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

Joined: Mar 06, 2001
Posts: 13459

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Package Statement.
 
Similar Threads
Packaging
Problem with protected variable...
How to create a package in Java?
problem on extending a class declared on the same package!!
default vs protected access