7 Replies - 451 Views - Last Post: 19 January 2013 - 11:23 AM Rate Topic: -----

#1 John-acme  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 65
  • Joined: 08-February 11

When should use Inner Class instead (normal) Class

Posted 18 January 2013 - 07:21 PM

I have question about this..

When we use separated class definition? when we use inner class?? What I know is one object, one class.. But sometime I want to connect/relate several object (maybe because objects have dependency each other) then I got problem because I need publicized a method to pass the value [getter method or setter method or both] (This will become vulnerable and can cause unwanted access). If I don't want to add getter or setter method then the solution I came out is using Inner Class.

Beside that I also want to know.. In my recent program, Database application, I separate GUI-Builder into a class and Database functionality into another class. And I face a problem in related these two classes(Database class need input/value from GUI and GUI need output/value to show to user).. Is this a good way of programming? or should I instead combine them into one class by put GUI-Builder Class as Inner Class in Database Class?

Thanks.

Is This A Good Question/Topic? 2
  • +

Replies To: When should use Inner Class instead (normal) Class

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 838
  • View blog
  • Posts: 2,575
  • Joined: 29-July 11

Re: When should use Inner Class instead (normal) Class

Posted 18 January 2013 - 07:35 PM

Good question. You will get different answers. I like to use inner classes when they will augment my class. Like ActionListeners.

Always keep your GUI and DAO classes separate.
Was This Post Helpful? 0
  • +
  • -

#3 John-acme  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 65
  • Joined: 08-February 11

Re: When should use Inner Class instead (normal) Class

Posted 18 January 2013 - 07:51 PM

different answer?? Do you mean everyone have their own preference??

Quote

I like to use inner classes when they will augment my class. Like ActionListeners.

I don't understand what you mean with 'augment your class' ..Yeah.. in most book ActionListener always as inner class.. and I think it is because ActionListener need direct access to object's attribute but outside of the object didn't need to access the ActionListener..

So let's say in another case beside Database's case.. Do you still suggest that GUI should be separated as an class ?

Thanks
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,071
  • Joined: 27-December 08

Re: When should use Inner Class instead (normal) Class

Posted 18 January 2013 - 07:53 PM

Yes. Data and State should be separate from the User Interface. Look into the MVC pattern. GregBrannon has an excellent tutorial: http://www.dreaminco...c-architecture/
Was This Post Helpful? 3
  • +
  • -

#5 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 838
  • View blog
  • Posts: 2,575
  • Joined: 29-July 11

Re: When should use Inner Class instead (normal) Class

Posted 18 January 2013 - 07:55 PM

GUI should always be separate. Your code should be able to work with any UI, text based or GUI.
Was This Post Helpful? 3
  • +
  • -

#6 John-acme  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 65
  • Joined: 08-February 11

Re: When should use Inner Class instead (normal) Class

Posted 18 January 2013 - 10:25 PM

I read the link that @macosxnerd101 given.. in that tutorial teach that Model and View didn't have direct access/link..In that example I can understand the teaching..
But now I try to apply in the very same database application which I define the Model-> have 3 functionality: Select, Update, Delete.. This model also can let user choose which database that user want to connect..

Now I have question, If the View didn't have direct access to Model then how can I show to user the databases that exist in start of the program... I mean in the very start, I have to provide at least databases that exist, but I cannot have direct access, so how to get the data? through the Controller??

Thanks
Was This Post Helpful? 0
  • +
  • -

#7 natecat  Icon User is offline

  • D.I.C Head

Reputation: 53
  • View blog
  • Posts: 225
  • Joined: 19-December 11

Re: When should use Inner Class instead (normal) Class

Posted 18 January 2013 - 10:29 PM

Inner classes aren't any different then normal classes, other that the container class can access private variables, and the inner class acts almost as if it is a subclass of the container class.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: When should use Inner Class instead (normal) Class

Posted 19 January 2013 - 11:23 AM

There are a lot a different answers and there are a lot of preferences and there are a lot of right answers too.

I use Inner class when what they contain do not need to be accessible to the outside world but only by the class inside which they are defined. The best example are ActionListener.

I start to add Inner classes when the number of lines in the father class starts to be too high.. now what too high means is left to your discretion
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1