• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Accessing a valiable outside the class

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have stored values in a List(public)...I want to use those values inside another class
without extending that class How can i?
 
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by shalini gnana:
I have stored values in a List(public)...I want to use those values inside another class
without extending that class How can i?




Make it as a static one then you can access with
 
shalini gnana
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to read but its not giving me the content of the list in my paint class..am i proceeding correctly?Where do i went wrong?
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have the list available with you. Why can't you try obtaining the iterator from the list as you have done in your Nodes class?

Please use the "Code" tags when pasting the java code. It will give a nice look and help the people much.
 
Marshal
Posts: 80224
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't set up an Iterator as a field; always use a local variable. The reason is that it can throw an Exception if other objects access the list.

Don't give a field public access; use a getList() method instead.
Are you trying to get the details to appear when you call some Swing component or other? How are you calling the paintcomponent method? If you are trying to do it the usual way, overriding the method, you have got two tiny subtle errors which are very difficult to see. 1: Give the method protected access, as in the Component class. 2: There is a tiny spelling error; you ought to use C instead of c. Prefixing the method with the "@Override" annotation is very useful; it would have thrown a compiler error.
It is unusual to call System.out.println() from inside paintComponent; you would usually put Strings on the Component with the Graphics object.

You have the wrong format for comments. Either //..... or /*....*/. Not //**....**/. You would get a compiler error if you split that comment into two lines.

See whether those corrections improve things. CR
 
shalini gnana
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the elements are added to the list in a method...while calling the method from another class should i have to call the method where the elements are got appended or just the variable(variable is declared as class variable)...If the how to call the method,mear calling will do?

Thank you....
 
Campbell Ritchie
Marshal
Posts: 80224
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure I quite understand your most recent question, but I shall try.

You can get at the List like this, and add elements:Or you can have a method in the Bar class like this:I think the second solution is much neater.
 
shalini gnana
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have add elements to list in a method and iterated and printed in another method.When i called the list variable in another class its empty...What can i do to get those values in the list?Where do i went wrong?
 
Campbell Ritchie
Marshal
Posts: 80224
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your two classes are completely confused.

The answer to your original question should have been "provide an accesor method" like this:That would however allow objects from other classes to do whatever they wish with your List.
I think the advice to make the List static was mistaken; you now have a single List which is shared between all instances of the class.
Have you sorted out the difference between paintComponent() which is used to set a display on a GUI component and System.out.print() which produces output on the command line or terminal?

I have already told you how to use methods to add to a List. In your latest example, you declare a field called list1 (bad name; you should be more specific) and then write list in your method. That suggests you have another variable, maybe even a local variable, which you are adding to. If it is a local variable, it will vanish altogether when the method completes. Also you are adding "node" which I can't understand.

For the iteration through the List and printing out its members, use a method which prints the List out.You ought to know how to traverse a Collection; look in the Java Tutorial (collections->interfaces->Collection interface) for more details.
[ January 05, 2008: Message edited by: Campbell Ritchie ]
 
grapes are vegan food pellets. Eat this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic