When will we use inner class

hi
when will we use inner class?

JosAH basically already wrote this, but another way
to say the same thing is this. If you are writing a
class and you find it would be handy to have another
class for that class to use, you can either write a
separate class for each, or you can embed one in the
other, making it an inner class. One advantage of an
inner class is that you can meet a set of
specifications by not having an extra class. It also
makes more sense to have an inner class if it's
something that's not useful on its own.I think it is the second most important advantage of inner class
(First is: you can access the private members of the outer class)

Similar Messages

  • Design question about when to use inner classes for models

    This is a general design question about when to use inner classes or separate classes when dealing with table models and such. Typically I'd want to have everything related to a table within one classes, but looking at some tutorials that teach how to add a button to a table I'm finding that you have to implement quite a sophisticated tablemodel which, if nothing else, is somewhat unweildy to put as an inner class.
    The tutorial I'm following in particular is this one:
    http://www.devx.com/getHelpOn/10MinuteSolution/20425
    I was just wondering if somebody can give me their personal opinion as to when they would place that abstracttablemodel into a separate class and when they would just have it as an inner class. I guess re-usability is one consideration, but just wanted to get some good design suggestions.

    It's funny that you mention that because I was comparing how the example I linked to above creates a usable button in the table and how you implemented it in another thread where you used a ButtonColumn object. I was trying to compare both implementations, but being a newbie at this, they seemed entirely different from each other. The way I understand it with the example above is that it creates a TableRenderer which should be able to render any component object, then it sets the defaultRenderer to the default and JButton.Class' renderer to that custom renderer. I don't totally understand your design in the thread
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=680674
    quite yet, but it's implemented in quite a bit different way. Like I was saying the buttonClass that you created seem to be creating an object of which function I don't quite see. It looks more like a method, but I'm still trying to see how you did it, since it obviously worked.
    Man adding a button to a table is much more difficult than I imagined.
    Message was edited by:
    deadseasquirrels

  • What is document date? When will you use it?in A/R invoice  in SAP B1

    Hi
    I would like to know the meaning of document date for A/R invoice  in sap b1.
    when will u use? and How will u use it?
    I know Posting Date is date when u add invoice
    Due Date is a last date that customer have to pay you.
    But I don't know document date.
    I know only it use to caculate in payment terms.
    Does document date in A/R invoice use when u closing-Period?
    Please guide it to me.
    Thank U
    Theerawat

    Hi Theerawat,
    You may check these threads first:
    Document Date
    Posting / Due / Document date trouble
    Re: Posting Date
    Re: Difference between posting & system date in Inventory Audit Report
    Thanks,
    Gordon

  • When will i use index organization table.

    when will i use index organization table.
    what is the advantage of these.

    See the sit
    http://www.dba-oracle.com/t_index_organized_tables.htm

  • Help,about why we use inner class?

    Hi,
    when i read "java Tutorial"
    i found there is one chapter about inner class .
    i copy it down as follow.
    the context is about there is a class Stack, and this class want to implement some function of interface Iterator,but as the book said
    we should not let class Stack implement the Iterator directly, we should add a inner class inside the Stack .
    i know it's very import ,but i still can not understand the reason why add a inner class here.
    hope somebody can explain it a little more for me or give an example.
    thank in advance!
    Iterator defines the interface for stepping once through the elements within an ordered set in order. You use it like this:
    while (hasNext()) {
    next();
    The Stack class itself should not implement the Iterator interface, because of certain limitations imposed by the API of the Iterator interface: two separate objects could not enumerate the items in the Stack concurrently, because there's no way of knowing who's calling the next method; the enumeration could not be restarted, because the Iterator interface doesn't have methods to support that; and the enumeration could be invoked only once, because the Iterator interface doesn't have methods for going back to the beginning. Instead, a helper class should do the work for Stack.
    The helper class must have access to the Stack's elements and also must be able to access them directly because the Stack's public interface supports only LIFO access. This is where inner classes come in.
    Here's a Stack implementation that defines a helper class, called StackIterator, for enumerating the stack's elements:
    public class Stack {
    private Object[] items;
    //code for Stack's methods and constructors
    not shown
    public Iterator iterator() {
    return new StackIterator();
    class StackIterator implements Iterator {
    int currentItem = items.size() - 1;
    public boolean hasNext() {
    public Object next() {
    public void remove() {
    or you can visit here
    http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html

    the context is about there is a class Stack, and this
    class want to implement some function of interface
    Iterator,but as the book said
    we should not let class Stack implement the Iterator
    directly, we should add a inner class inside the
    Stack .Simply because the implementation of the Iterator is nobody's business. By declaring it to be a private inner clss, nobody will ever know about it and only see the Iterator interface.

  • Help: Factory Class using Inner Class and Private Constructor?

    The situation is as follows:
    I want a GamesCollection class that instantiates Game objects by looking up the information needed from a database. I would like to use Game outside of GamesCollection, but only have it instantiated by GamesCollection to ensure the game actually exist. Each Game object is linked to a database record. If a Game object exist, it must also exist in the database. Game objects can never be removed from the database.
    I thought about making the Game object an inner class of GamesCollection, but this means that Game class constructor is still visible outside. So what if I made Game constructor private? Well, now I can't create Game objects without a static method inside Game class (static Object factory).
    Basically what I need is a constructor for the inner Game class accessible to GamesCollection, but not to the rest of the world (including packages). Is there a way to do this?

    leesiulung wrote:
    As a second look, I was initially confused about your first implementation, but it now makes more sense.
    Let me make sure I understand this:
    - the interface is needed to make the class accessible outside the outer classBetter: it is necessary to have a type that is accessible outside of GameCollection -- what else could be the return type of instance?
    - the instance() method is the object factory
    - the private modifier for the inner class is to prevent outside classes to instantiate this objectRight.
    However, is a private inner class accessible in the outer class? Try it and see.
    How does this affect private/public modifiers on inner classes?Take about five minutes and write a few tests. That should answer any questions you may have.
    How do instantiate a GameImpl object? This basically goes back to the first question.Filling out the initial solution:
    public interface Game {
        String method();
    public class GameCollection {
        private static  class GameImpl implements Game {
            public String method() {
                return "GameImpl";
        public Game instance() {
            return new GameImpl();
        public static void main(String[] args) {
            GameCollection app = new GameCollection();
            Game game = app.instance();
            System.out.println(game.method());
    }Even if you were not interested in controlling game creation, defining interfaces for key concepts like Game is always going to be a good idea. Consider how you will write testing code, for example. How will you mock Game?

  • Why and how to use "inner class"

    When i am learning advanced java language features,
    i could not understand why and when to use the "inner class",
    who can give me some examples?
    Thanks!

    You would use an inner class when an object needs visibility of the outer class. This is akin to a C++ friend.
    An example of this is an iterator. An iterator over some collection is typically implemented as an inner class of the collection class. The API user asks for an Iterator (from the Collection) and gets one - in fact they receive an instance of an inner class, but doesn't care. The iterator needs to be inner, as the iterator needs to see the internal data structures of the outer (collection) class.
    This could also be done with an anonymous class, as mentioned by spenhoet above. However, in the case of a collection, the role of the iterator is clear - thus it deserves its own class. And often there is more than one place an iterator can be returned (e.g. see java.util.List, which has several methods that return Iterators/ListIterators), thus it must be put in its own class to allow reuse of the code by outer class.

  • Help! - JRE can't find classes that use inner classes!

    Whenever I try to launch an app using the JRE, if the class was built with an inner class definition, the JRE says it can't find the class.
    For instance:
    I created a class called GUI, which contains anonymous inner class definitions for the button handlers: addActionListener(new ActionListener()){, etc. 
    When I compile this class I get GUI.class and GUI$1.class
    When I try to launch this using the JRE:
    jre -cp c:\progra~1\myApps GUI
    The JRE spits out
    'Class not found: GUI'
    BUT if I go back and remove the action handlers so that the only compiled class file is GUI.class (not GUI$1.class) then the JRE can find the class no problem.
    What in the good Lord's name is going on here?

    Thanks for the response. Got the 1.2.2 version of the JRE, where I guess you no longer invoke jre.exe (no such file exists) but java.exe instead (not well documented).
    Also, the newer version of the JRE has no problems locating classes, so I guess I'm OK.

  • How to use inner class in axis2 in java?

    Hi,
    Iam very new in axis2. My language is java. What iam trying to do is, i need to create a xml format for the clients to send data to server using axis2 service.
    My demo xml format is,
    <test1>
    <test2>
    <test3>
    <test4></test4>
    <test5></test5>
    </test3>
    <test3>
    <test4></test4>
    <test5></test5>
    </test3>
    </test2>
    </test1>
    Iam trying to use nested inner class to generate this xml. Below is my demo java class. But unfortunately, or may be lac of knowledge, iam unable to create sub trees.
    public class EchoService{
    public MyInnerClass retMyInnerClass(MyInnerClass test1) {
    return test1;
    public class MyInnerClass {
    private String test2;
    public MyInnerClass() { }
    public void setTest2(String test2) {
    this.test2 = test2;
    public String getTest2() {
    return this.test2;
    Please help me to generate the web service for client.
    Thanks in advance.

    847897 wrote:
    well, i apologies for that fault. But i need the answer. It is urgent.That's your problem, not ours. However, I don't see the connection between your inner class and producing XML subtrees. Perhaps if you explained the problem, rather than your solution...
    [url http://sscce.org/]This page is usually a good place to start; otherwise [url http://www.catb.org/~esr/faqs/smart-questions.html]this one.
    Winston

  • Use inner class as collection

    Is there a way to get toplink to use an inner class collection to house my 1:m relationship. The collection needs some attributes of the containing object.
    Can i override how collection is constructed within the java, because it seems pretty clear the workbench doesn't support this.
    thanks
    craig

    Seem like an odd thing to do. If it were a static inner class it would be possible, but an instance inner class is probably not.
    If you really need a reference to the source object from the collection object, it might be better to accomplish this through making the inner collection class an outer class, or at least static. You could then set a back reference to the source object in the source object's set method for the collection. You would need to configure the mapping to use method access, or if using value holders you could lazily set the collection back reference from your get method for the collection. You could also make special get/set methods for TopLink to use to access the collection value that convert the special collect to and from a normal collection.
    In general it might be better to put the logic specific to the owning class in the owning class instead of the collection.

  • Please give me the information when to use inner classes in applications

    HI,
    please give me when to use the innerclasses

    > HI,
    please give me when to use the innerclasses
    Use them when you want to create a class within another class.
    Now gimme all 'er Dukes!!!

  • When should I use abstract classes and when should I use interfaces?

    Can any body tell me in which scenario we use /we go for Interface and which scenario we go for abstract class, because as per my knowledge what ever thing we can do by using Interface that thing can also done through abstract class i mean to say that the
    behavior of the two class.
    And other thing i also want to know that which concept comes first into the programming abstract class or Interface.
    S.K Nayak

    The main differences between an abstract class and an interface:
    Abstract
    An abstract class can contain actual working code (default functionality), and can have either virtual or abstract method.
    An abstract class must be sub-classed and only the sub-classes can be instantiated. Abstract methods must be implemented in the sub-class. Virtual methods may be overridden in the sub-class (although virtual methods typically contain code, you still may
    need/want to override them). A good use for an abstract class is if you want to implement the majority of the functionality that a class will need, but individual sub-classes may need slightly different additional functioality.
    Interface
    An interface only contains the method signatures (method name and parameters), there is no code and it is not a class.
    An interface must be implemented by a class. An interface is not a class and so it cannot be sub-classed. It can only be implemented by a class. When a class implements an interface, it must have code in it for each method in the interface's definition.
    I have a blog post about interfaces:
    http://geek-goddess-bonnie.blogspot.com/2010/06/program-to-interface.html
    (sorry, I have no blog posts specific to abstract classes)
    ~~Bonnie DeWitt [C# MVP]
    http://geek-goddess-bonnie.blogspot.com

  • Q: General : When will you use Web Service as opposed to just Servlet and/or EJB?

    Hi,
    I don't know if this is an appropriate question to ask for this class as it's not
    workshop
    specific. I was wondering how will I decide when it is appropriate to use a web
    service as opposed to say servlet or an EJB for my application? What is a good
    rationale to use?
    Thanks,
    Sadhana

    Hi Adam,
    Thanks a lot for giving such a lucid explanation of my vague question!
    I understand now the difference between when to use EJBs/Servlets/Web Services.
    Thanks again!
    Sadhana
    "Adam FitzGerald" <[email protected]> wrote:
    >
    Hi Sadhana,
    Web service versus Servlet versus EJB. Which one to use? That is a very
    good question.
    EJBs should be the core desription and processing of your business rules.
    They generally
    have good performance, are easy to integrate into your security set up but
    they require
    access using the RMI-IIOP protocol. This means that generally EJBs are not
    accessible
    outside of your firewall and their clients have to be written in Java (other
    enterprise
    objects or applets or GUI applications).
    Servlets often form a web-based front end to EJBs. They also have good performance,
    integrate into security and only require HTTP access. This means they are
    accessible
    outside your firewall and any client with a HTTP or web API can call on
    them, however,
    there is no standard contract or rules for this type of interaction so you
    will need
    provide direct input into development of the client.
    Web services are also accessible via HTTP and so can be accessed from outside
    your
    firewall. In fact, Web services internally within Workshop and WebLogic
    actually
    operate by using servlets and EJBs but the main advantage is that the mechanism/contract
    for interacting with the web service is a well defined standard (WSDL) and
    can actually
    be registered or retreived dynamically (UDDI). Since web service standards
    are open,
    most popular programming languages already provide client APIs to interact
    with them
    thus freeing the web service developer from direct interaction with the
    client developer
    and letting the client be developed on any platform. The penalty for this
    openness
    is a small performance hit (XML translation) and a slowly maturing security
    model.
    So in summary, use EJBs for business operations that have Java clients within
    your
    enterprise, use servlets to provide web style access to your business operations
    (like web pages) and use web services to enable automated clients that can
    be written
    on any platform either inside or outside your enterprise (firewall). In
    fact, it
    is common to write the EJB first and then to expose some parts of it outside
    of your
    enterprise by writing a web service wrapper (maybe using the EJB controls
    that we
    saw in class).
    I hope this helps.
    Adam
    "Sadhana Jain" <[email protected]> wrote:
    Hi,
    I don't know if this is an appropriate question to ask for this class as
    it's not
    workshop
    specific. I was wondering how will I decide when it is appropriate to use
    a web
    service as opposed to say servlet or an EJB for my application? What is
    a good
    rationale to use?
    Thanks,
    Sadhana

  • Get error when unzipping document using Java Class

    I am using an applet to unzip a compressed docuement. The document is compressed by a Java Rich client into a database and is going to be opened by a web application. So in order to decompress we are using an applet with the same unzip code. The rich client is working fine, but the applet is giving an error. Can anyone tell me what the following error means?? I have found no documentation on it and it is not giving me any real information.
    java.util.zip.DataFormatException: oversubscribed dynamic bit lengths tree
    I am using the Inflater class to unzip the doc.
    thanks,

    Can you be any more specifc about what you are doing?
    It may be a security issue as you are running the code in an applet.
    The other thing to look into would be the content type you are using to send the zipped data over http. When writing the zipped object into the HttpServletResponse on the server you must call
    setContentType("application/zip");
    For a quickish example see
    http://java.sun.com/developer/technicalArticles/Networking/Webserver/WebServercode.html
    This sets the content type based on the file to be returned extension.
    Hope this is helpful.

  • When will I use object locking by User ID report?

    Hello,
    I am using EP7 SP9.
    I would like to see all the locked users at the portal.
    The problem is that even that there are locked users (I can locate them individually using the UME), the object locking report doesn't show them when I search by User ID.
    I know I can search it at the UME advanced search, so what the object locking by User ID is used for?

    Hi,
    You can view all the locked objects in your portal under System Administration>>Monitoring. Under the Portal tree node, click on the Object Locking. You can enter the search criteria to UserId and perform the search operation.
    Regards,
    Sujana

Maybe you are looking for

  • Function Module which will take Wekk No & Return date range (from & To dat)

    Hello everyone, I need a Function Module which will take Week No as an input Parameter & return me the date range (From date & To date of that week) . Thanks in advance! Cheers! Moderator message: date calculation questions = FAQ, please search befor

  • Enhancement /Demand in MD15

    HI Experts, My client is telling that previously in MD15 he can able to select 2000 nos collective conversion of planned order to Purchase requestion and now he is telling in configuration it is only 500nos @ a time he can select. I do check in OMDV

  • Resizing to fill the window.

    I would like my application resize to fill the available canvas, but am unable to figure out to make my StackPane expand to fill the space. My latest attempt is using binding, but I am getting a "A bound value cannot be set." exception on what I figu

  • More smaller disks or fewer larger ones?

    Hi: We're in the middle of procuring disks for a new Oracle server. We plan to stripe disks to randomly distribute I/O load across disk heads. There is some debate on whether or not we should use fewer large disks vs more smaller ones. For example, t

  • Problem in web service while applying patches

    Hi, While applying basis patches from SAPKB70012 to SAPKB70016 we were getting abap dump. i coordinated with SAP and was able to resolve the abap dump. But there are still errors in spam. Currently none of my webservice are working "Service  Cannot B