Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Help

Options
  • 13-10-2010 11:27pm
    #1
    Registered Users Posts: 17,965 ✭✭✭✭


    Just have two questions;

    1. Can anyone explain in black and white, the use of wrapper classes and why they're needed?

    2. Nested/Inner Classes I know what these are, but just wondering how do you decide when to have the class/varibles public,etc...? Also Local and Anonymous ones, what are they about?


    Cheers in advance.


Comments

  • Registered Users Posts: 981 ✭✭✭fasty


    1) I don't really do much Java, but a wrapper class will often encapulate or abstract the functionality of other classes. Does Java have namespaces? That's another reason for wrapper classes - to group stuff together.

    2) Have classes and variables public if you want others objects to access them.


  • Registered Users Posts: 17,965 ✭✭✭✭Gavin "shels"


    What do you mean by namespaces?


  • Registered Users Posts: 130 ✭✭never2fast


    Java do not use namespaces, namespaces is used in language such as Adobe Flex.

    Wrapper class are used to abstract functions. For example you have a class that connect to and create entries in a database in two separate methods a wrapper can combine the connect and create into a single method. so to the user of your wrapper class they can call one method instead of two. Good wrapper methods may wrap up much more than two methods (for example may have validation in there too before creating the entry to the DB)


  • Registered Users Posts: 130 ✭✭never2fast


    To understand modifiers (private, public default, strictftp) you will need to understand more about object oriented programming. In general they govern what another class can see within the declaring class, for example a extended class cannot see anything marked private on the parent class but can see the class variables marked with the other three. They have the same effect when declared for class variable and class methods.


  • Registered Users Posts: 1,998 ✭✭✭lynchie


    What do you mean by namespaces?

    Namespaces allow you to logically group common symbols / classes etc into one group, thus allowing you to use the same name in another group without conflict.

    In java, the concept of namespaces is covered under the package keyword. C# uses the using keyword.
    never2fast wrote: »
    Java do not use namespaces, namespaces is used in language such as Adobe Flex.

    Java uses packages which is the same concept.

    Flex uses both the package keyword for namespacing classes in AS3 and uses xml namespaces in the mxml.


  • Advertisement
  • Registered Users Posts: 981 ✭✭✭fasty


    Ah I didn't realise Java had packages. Sorry for confusing you, op! I've seen languages without namespaces use wrapper classes as a substitute!


  • Registered Users Posts: 17,965 ✭✭✭✭Gavin "shels"


    Cheers, info here and a bit off google has me sorted thanks.


  • Registered Users Posts: 14,339 ✭✭✭✭jimmycrackcorm


    Wrapper classes can be more logically identified as adapter design patterns. Their purpose is to create a common access interface for disparate classes.


  • Closed Accounts Posts: 410 ✭✭JohnathanM


    Wrapper classes can be more logically identified as adapter design patterns. Their purpose is to create a common access interface for disparate classes.

    Just to confuse the OP, you could also define a decorator as a wrapper. :)

    OP, as an example of use when I introduce a new library to a large project the first thing I will do is write an adapter for it. I will provide an interface to my application, and through that call the library. Lets say I import Apache's StringUtils and it has a static method isNumeric:

    public static boolean isNumeric(String theString) {
    return ApacheClass.isNumeric(theString);
    }

    In this way, if I ever need to change the library I only need change the call behind my own class, and not throughout the application:

    public static boolean isNumeric(String theString) {
    return NotAnApacheClass.isTheStringNumeric(theString);
    }

    It doesn't need to be as complicated as reconciling numerous classes (which tends toward facade), or consolidating numerous methods (which needs to be handled carefully as you risk merging responsibilities).


Advertisement