| Author |
Packgage problem
|
sarvesh dikonda
Ranch Hand
Joined: Apr 08, 2012
Posts: 58
|
|
I came across a question in which it was stated that, classes within the same package can directly access and change the public instance variables within these classes. So, i decided to just try it out as i wanted to see how exactly it is to be done. Below is the code i implemented -
I get the following error -
I'm finding difficult to solve the above error. I know its not that difficult, but need your expert help friends.
I want class Tte to access the String variable 'name' from the class Tt. Both the classes are placed in same package dd. I tried different ways as seen the commented lines in class Tte i.e the import statement but i am not able to understand how to do it. Can someone help me out please!
|
Always believe in yourself
|
 |
Himai Minh
Ranch Hand
Joined: Jul 29, 2012
Posts: 287
|
|
try this:
and
See if it compiles.
|
 |
sarvesh dikonda
Ranch Hand
Joined: Apr 08, 2012
Posts: 58
|
|
Himai Minh wrote:try this:
and
See if it compiles.
On compiling the code with your suggested changes it does not work and gives the error as follows-
The changed code as follows -
Kindly help me friends i tried with other ways too and am also looking into the problem.
I have a folder named "dd" in which i have placed the Tt.java and Tte.java source files.
And then i have compiled them as following - C:\Documents and Settings\User\Desktop>javac dd\Tt.java // compiles properly
C:\Documents and Settings\User\Desktop>javac dd\Tte.java // on compiling it shows 3 errors as shown above
From the above as you can see, i am compiling from my current directory-> Desktop
Am i compiling correctly , please correct me if i am wrong
|
 |
sarvesh dikonda
Ranch Hand
Joined: Apr 08, 2012
Posts: 58
|
|
Kindly help me with this problem Ranhers! I need to know how this thing goes on to work
|
 |
Kancharla Madhu
Ranch Hand
Joined: May 06, 2012
Posts: 93
|
|
Your trying to access instance variable without actual object instance so Compilation Error! i.e
Do one of the following things.
1) Declare variable, name as static i.e.
2) Create Object instance and then invoke the variable name. i.e.
|
Champions aren“t made in the gyms. Champions are made from something they have deep inside them - a DESIRE, a DREAM, a VISION
|
 |
sarvesh dikonda
Ranch Hand
Joined: Apr 08, 2012
Posts: 58
|
|
Kancharla Madhu wrote:Your trying to access instance variable without actual object instance so Compilation Error! i.e
Do one of the following things.
1) Declare variable, name as static i.e.
2) Create Object instance and then invoke the variable name. i.e.
Thank you so much Madhu for your solution . Actually, i knew the first solution that you suggested with 'static' keyword but not about the second one. Thanks. Would you like to give some tips for the OCPJP certification exam.
|
 |
Guy Buyle
Greenhorn
Joined: Nov 30, 2012
Posts: 6
|
|
Hi,
I think your problem is more related to the javac line command:
I have tried the following:
your sources in: dd
When I compile your sources wth the following command line:
the class Tt compiles fine, no compilation errors but the second compilation line get the following error:
I think this is the correct answer on your question.
Regards,
Guy
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1776
|
|
Hi GuyB,
Welcome to Javaranch
I have edited your post to have Code Tags in them to make it easily readable. Check how to use them here
|
 |
 |
|
|
subject: Packgage problem
|
|
|