Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Exposing lists as class properties / attributes

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Exposing lists as class properties / attributes

    Hi folks.

    Hopefully I won't mess up on my first post but here goes.

    I'm currently in the process of migrating my VB / VB.NET knowledge of 12 years to Java, using Netbeans as my chosen IDE. Things are starting to sink in but I'm currently stuck with one thing which I've taken from granted in VB but can't seem to achieve in Java.

    In VB.NET you can expose generic collections as properties of a class. For example if we have a class called MyProject, you can have a property called 'Components' which is simply a reference to a collection within the class. This allows you to reference the collection's properties and methods directly in the following way ('Contains' is a property of many VB.NET generic collections)...

    MyProject.Components.Contains(MyComponent)

    I know that in Java you use the concept of 'getters' and 'setters', but what would be the accepted method of achieving the above functionality, within the standards of the Java language?

    Any help appreciated.

    Thanks.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Exposing lists as class properties / attributes

    I guess I'm not understanding what you're looking for, but can't you just use a List?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exposing lists as class properties / attributes

    How about having a get method that returns a reference to the property.

    The way you have coded it looks like how defining the property as public static would work.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Exposing lists as class properties / attributes

    Quote Originally Posted by KevinWorkman View Post
    I guess I'm not understanding what you're looking for, but can't you just use a List?
    Hi Kevin and thanks for the response.

    I'm don't think I quite understand your response, but are you saying that the list should just be declared and publicly accessible without a 'getter' and 'setter'? I know in VB.NET that this principle is generally frowned upon, and that all attribute member variables should only be set and retrieved via a Property Get or Property Set method.

    Quote Originally Posted by Norm
    Re: Exposing lists as class properties / attributes

    How about having a get method that returns a reference to the property.

    The way you have coded it looks like how defining the property as public static would work.
    Hi Norm.

    Is it valid to have member variables declared directly without implicit methods to get and set them?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exposing lists as class properties / attributes

    Is it valid to have member variables declared
    yes, but it is not good coding practice. It violates some OOP principles.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Exposing lists as class properties / attributes

    That's what I thought!

    In VB.NET a collection object is a reference type; the object is never physically moved or copied. All you pass around when dealing with collections is a pointer to that collection. This is probably why VB.NET lets you interact with the object directly using the syntax
    MyClass.MyCollectionProperty.MyCollectionPropertyMethod()

    I'm assuming then that there is no way to do this in Java without cheating (i.e. setting the collection variable to public and accessing it directly)? If I wanted to be 100% OOP compliant I'd have to do the following (not syntactically correct but hopefully you'll understand)...

    MyClass Definition

    private collection myCollection;
     
    private collection getMyCollection() {
        return myCollection;
    }
     
    private void setMyCollection(collection collectionOject) {
        myCollection = collectionObject;
    }

    Calling Code

    private collection SomeCollection;
    private MyClass SomeClass;
     
    public void main() {
     
        // Create new instance of class
        SomeClass = new MyClass;
     
        // Copy the object to a local instance variable
        SomeCollection = SomeClass.getMyCollection();
     
        // Set some properties and / or do stuff
        SomeCollection.setThisProperty = PropertyValue;
        SomeCollection.Method();
     
        // Replace the collection in the class instance with the modified one
        SomeClass.setMyCollection = SomeCollection;
     
    }

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Exposing lists as class properties / attributes

    Java coding conventions uses lowercase first letters for variables:
    private MyClass someClass;

    A constructor is called like a method(with ()s)
    someClass = new MyClass();
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Exposing lists as class properties / attributes

    Is the VB code somehow more OOP-compliant than what we've been suggesting?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Exposing lists as class properties / attributes

    Quote Originally Posted by KevinWorkman View Post
    Is the VB code somehow more OOP-compliant than what we've been suggesting?
    Not sure but this may well be the case. Simply referencing a variable from within a class as a public entity is definitely frowned upon, but achieves the same result from a functional perspective.

    Public Class MyClass
     
        ' Publicly exposed member variable of MyList class
        Public Shared MyList As Generic.List(of String)
     
    End Class

    ... is fundamentally the same as ...

    Public Class MyClass
     
        ' Private member variable in which to store the property value - cannot be seen directly by calling code
        Private _myPrivateList As Generic.List(of String)
     
        ' Property construct exposing Get and Set functionality
        Public Property MyList() As Generic.List(of String)
            Get
                Return _myPrivateList
            End Get
            Set(ByVal value As Generic.List(of String))
                _myPrivateList = value
            End Set
        End Property
     
    End Class

    However like Norm said, the first example doesn't conform to true OOP methods, although it is perfectly functional so far as the program is concerned. To demonstrate how the property is accessed, lists in VB.NET have a Contains(entity) method which returns TRUE if the list contains the entity supplied (a string for example). In both the above examples the Contains() method would be called by using...

    If MyClass.MyList.Contains(aValue) = True Then ...

    If you guys were wanting to write a class that would expose a list of other classes to a calling program (e.g. A folder class containing a list of file clases for each file within that folder), how would you achieve this in the accepted Java fashion? If you wouldn't do it anything like what I'm wanting to achieve, please let me know with an example.

    Thanks for all the help so far.

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Exposing lists as class properties / attributes

    In your VB code, is MyClass an instance, or is it simply a class? I don't know how VB works, but you might be asking two different things depending on what exactly MyClass is. Is it a class, or an instance of a class?

    Java's List interface also has a contains() method.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Exposing lists as class properties / attributes

    By the look of it your VB code exposes the collection as a public piece of data. Ie, calling it myPrivateList is a bit misleading as it isn't, in any real sense, private. Precisely because the two forms you give are functionally equivalent one is neither more nor less "OOP" than the other. Anyone who would frown on one but smile on the other is following a rule, not exercising judgement.

    The same functionality would be achieved in Java by making it a public member variable. (The difference between the languages is that VB has a bit of sugar allowing you to write functionality like a method, but invoke it like a field. Java programmers would probably dislike the taste of the sugar: preferring to see from the syntax what manner of thing they are dealing with. But it is a matter of taste.)

    In Java collections are a reference type, and are passed about as copies of pointers. The languages don't really differ in that respect.

    Rather than worrying about whether this is OOP or whatever, it might be more instructive to consider what could go wrong (in either language)

    (1) An instance of MyClass can have its collection data changed to a different collection by another class. And it won't know anything about it. The solution here is to provide a setter that responds to the change, or not have a setter in which case the collection really will be private in the sense that no-one outside the class can alter which collection is being used. (In VB the response would go in the Set part, or there could be no Set functionality.)

    (2) An instance of MyClass can have the contents of its collection data altered without knowing anything about it. Ie the caller can add or remove things from the collection once it has a reference to it. The solution here is to provide a getter that returns a copy of the collection, so callers can only monkey about with the copy, not the collection that is part of the MyClass instance. Or you could not provide a getter at all but provide specific methods to allow specific behaviour involving the collection.

    This last alternative (no getter or setter) even hides the state of the things in the collection from the caller! The caller asks for something to be done: it doesn't ask to be told what the MyClass instance's data is.

  12. The Following User Says Thank You to pbrockway2 For This Useful Post:

    CodeProphet (December 12th, 2012)

Similar Threads

  1. How to load class path for .properties file
    By kewlkeny in forum Web Frameworks
    Replies: 1
    Last Post: January 23rd, 2012, 08:40 AM
  2. attributes and properties in jsp page
    By god1gracious in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: December 30th, 2011, 03:31 AM
  3. key with 2 attributes?
    By ober0330 in forum Collections and Generics
    Replies: 1
    Last Post: October 10th, 2011, 11:47 AM
  4. Get CSS Attributes using Java
    By justinmifsud in forum Object Oriented Programming
    Replies: 2
    Last Post: February 15th, 2011, 05:08 PM
  5. Cannot access class attributes
    By Cyburg in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 29th, 2010, 07:30 AM