• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Packgage problem

 
Ranch Hand
Posts: 58
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try this:


and


See if it compiles.
 
sarvesh dikonda
Ranch Hand
Posts: 58
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kindly help me with this problem Ranhers! I need to know how this thing goes on to work
 
Ranch Hand
Posts: 109
Eclipse IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Let me tell you a story about a man named Jed. He made this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic