Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How can a child access parrent's instance variable in Java?  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a bloated which has  I want to add yet another button . When pressed it takes c1-cN, processes them and returns result. I can squeeze it in this class (just add 1000 lines of methods, starting with and access instance variables c1-cN directly), but I want to move it to a separate class for readability purposes.

I see several ways to do this:
(1) Pass c1-cN to constructor (simple, but requires too many arguments)
(2) Make c1-cN static (this is ugly)
(3) Say "hey, method, go to your parent and ask, what was his c1 value".  //this looks cleanest, but how to implement it?

Which method should I use?
 
Saloon Keeper
Posts: 8580
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe there's an opportunity to make your class more generic instead. Maybe you can tell us whether N is a constant, what all the classes do, what the computeResult() method is supposed to do with them, etc.

It also strikes me that you have way too much business logic in your presentation layer. If you want to split your logic up, you should separate your layers and move your code to the appropriate place.
 
Bartender
Posts: 1771
45
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Stephan mentioned below, you need to specify what N stands for.

Here is a typical structure:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!