Results 1 to 9 of 9
  1. #1
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default static and a simple constructor

    im using this class for password checking but when i use a constructor to construct a static array variable, in the program where i use this throws a NullPointerException, but i tried to make a static initializer and initializes that variable everything works fine,,

    does it mean that constructors are only for instances? instance data member(fields)?

    Java Code:
    public class Password {
    
        private static String[] passwords;
    
        static {
    
            passwords = new String[] {"123", "ABC"};
        }
       
        /**
             public Password() {
    
                passwords = new String[] {"123", "ABC"};
             }
        */
    
        public static boolean isPasswordAuthentic(String password) {
    
            boolean isAuthentic = false;
            
            for (int x = 0; x <= passwords.length - 1; x++) {
    
                if (password.equals(passwords[x])) {
    
                    isAuthentic = true;
                    break;
                }
            }
    
            return isAuthentic;
        }
    
        public static void addNewPassword(String newPassword) {
    
            // UNSUPPORTED
            throw new UnsupportedOperationException();
        }
    
        public static void deletePassword(String password) {
    
            // UNSUPPORTED
            throw new UnsupportedOperationException();
        }
    }

  2. #2
    mardrew is offline Member
    Join Date
    Jan 2010
    Posts
    1
    Rep Power
    0

    Default static and simple constructor answer

    Yes a constructor will only run when an instance of a class is created. It sounds like you just called Password.isPasswordAuthentic directly. If so the reason you got the null pointer exception on line

    for (int x = 0; x <= passwords.length - 1; x++) {

    was that the passwords array was not initialised. Hence it was calling passwords.length on a null object and failing.

  3. #3
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default

    ahh, so sir.. this is the only efficient way to initialize a static variable? by defining a static initializer?. thank you for the response :)

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    Well no, you could always simply do:

    private static String[] passwords = new String[] {"123", "ABC"};

    By the way, if you intend on having the number of passwords vary then you probably want to use a List of some description.

  5. #5
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default

    I beg your pardon please let me correct my question , what i mean is not "only"
    , but is it one of the efficient way of "initializing" a static member?

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    I don't know if there's an overhead to the static block, but in this case the usual thing would be to initialise it the way I did. A static block is usually used for more complex initialisation work (loading properties for example). Doing simple initialisations in a static block you run the risk of the initialisation being separated from the variable in the code, not in terms of the way it's run, but visually, which makes it harder to debug stuff.

  7. #7
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default

    I don't know if there's an overhead to the static block, but in this case the usual thing would be to initialise it the way I did. A static block is usually used for more complex initialisation work (loading properties for example). Doing simple initialisations in a static block you run the risk of the initialisation being separated from the variable in the code, not in terms of the way it's run, but visually, which makes it harder to debug stuff.
    thank you so much sir... i was just asking these because im not familiar with static blocks, it just came up in my mind on how to initialize static variables,
    because i use to define a constructor where the instance variables are initialized, so i was thinking of ..... -"how can i initialize this static variable",
    and i never use to initialize most of my instance variable immediately when it is created,

    oh and by the way sir.. would you mind to look at this one
    Java Code:
      
      public Password() {
    
            passwords = new String[] {"123", "ABC"};
        }
    just for some clarification, i like to hear your comment with this, a contructor that initializes a static member,

    oh and this one, i got when i was reading a page from google a method that initialize a static member
    Java Code:
    private static <datatype> <indentifier> = initializeMyStaticObject();
    i notice that i can use this one, so in the later part i can initialize again a static member...

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,016
    Rep Power
    20

    Default

    I can't, offhand, think of a good reason to initialise a static variable in a constructor, and I can think of a couple of good reasons not to.
    First is you would initialise that variable everytime you created a Password object.
    Second is it's just wrong...the static variable is simply not part of an object.

    Using a method to initialise a static is a reasonable option, but again don't do it for trivial things (like the array example).

  9. #9
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default

    Using a method to initialise a static is a reasonable option, but again don't do it for trivial things (like the array example).
    wooops!

    im about to write it.. thank your sir.....!

Similar Threads

  1. Static Builder and Create Methods for simple class
    By Pilot Ace in forum New To Java
    Replies: 3
    Last Post: 08-30-2009, 10:18 AM
  2. Replies: 5
    Last Post: 02-04-2009, 01:20 AM
  3. [SOLVED] static block or constructor?
    By JT4NK3D in forum New To Java
    Replies: 3
    Last Post: 05-27-2008, 02:21 PM
  4. Demonstration of a simple constructor
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:47 PM
  5. Replies: 0
    Last Post: 12-19-2007, 09:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •