Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Packgage problem

 
sarvesh dikonda
Ranch Hand
Posts: 58
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Himai Minh
Ranch Hand
Posts: 1310
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this:


and


See if it compiles.
 
sarvesh dikonda
Ranch Hand
Posts: 58
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kindly help me with this problem Ranhers! I need to know how this thing goes on to work
 
Kancharla Madhu
Ranch Hand
Posts: 109
Eclipse IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sarvesh dikonda
Ranch Hand
Posts: 58
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic