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 variables.

Options
  • 21-01-2011 11:57am
    #1
    Closed Accounts Posts: 6,281 ✭✭✭


    Hey all,

    Need some help, It's not a major issue but it's something that's bugging me a bit.

    If I make a java class, with class attributes( variables ) what should their status be?
    public class Numbers {
      
      private String numberOne = "one";
      private String numberTwo = "two";
      private String numberThree = "three";
      private String numberFour = "four";
      protected ArrayList<String> arrayListName = new ArrayList<String>();}
    

    So all these attributes here are being added into the arraylist I know that private works fine for this but is there some coding rules that states they should be private or is it personal preference and they could be public? They don't contain anything important.

    Also the protected ArrayList this is used by child classes but also contains nothing important so does it need to be done?

    Also what is the correct name for "Public" "Private" "Protected" is it status?

    Any links/comments welcome

    Thanks :)


Comments

  • Registered Users Posts: 1,112 ✭✭✭Dacelonid


    Ricky91t wrote: »
    Hey all,

    Need some help, It's not a major issue but it's something that's bugging me a bit.

    If I make a java class, with class attributes( variables ) what should their status be?
    public class Numbers {
      
      private String numberOne = "one";
      private String numberTwo = "two";
      private String numberThree = "three";
      private String numberFour = "four";
      protected ArrayList<String> arrayListName = new ArrayList<String>();}
    

    So all these attributes here are being added into the arraylist I know that private works fine for this but is there some coding rules that states they should be private or is it personal preference and they could be public? They don't contain anything important.

    In pure programming terms it is up to you as the developer to decide if they should be public, private or protected. However from a design point of view (Object Orientated) they should be private, and you should provide get and set methods to access them. That isn't true in 100% of cases, ie if you subclass this class you might want some of those to be protected.
    Ricky91t wrote: »
    Also the protected ArrayList this is used by child classes but also contains nothing important so does it need to be done?
    Without seeing the rest of the code, we don't know what the ArrayList is for or how you are using it, so can't comment on whether it should be private etc.
    Ricky91t wrote: »
    Also what is the correct name for "Public" "Private" "Protected" is it status?
    They are access modifiers.
    See here for the different options.
    http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html


  • Closed Accounts Posts: 6,281 ✭✭✭Ricky91t


    Thanks Dacelonid, the array list is just accessed by getter and setters in child classes, but I understand now and it should stay protected, cheers!


Advertisement