Results 1 to 9 of 9
Thread: static and a simple constructor
- 01-29-2010, 08:55 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 68
- Rep Power
- 0
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(); } }
- 01-29-2010, 10:00 AM #2
Member
- Join Date
- Jan 2010
- Posts
- 1
- Rep Power
- 0
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.
- 01-29-2010, 11:25 AM #3
Member
- Join Date
- Dec 2009
- Posts
- 68
- Rep Power
- 0
ahh, so sir.. this is the only efficient way to initialize a static variable? by defining a static initializer?. thank you for the response :)
- 01-29-2010, 12:31 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 01-29-2010, 03:55 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 68
- Rep Power
- 0
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?
- 01-29-2010, 04:14 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 01-29-2010, 04:30 PM #7
Member
- Join Date
- Dec 2009
- Posts
- 68
- Rep Power
- 0
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,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.
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
just for some clarification, i like to hear your comment with this, a contructor that initializes a static member,Java Code:public Password() { passwords = new String[] {"123", "ABC"}; }
oh and this one, i got when i was reading a page from google a method that initialize a static member
i notice that i can use this one, so in the later part i can initialize again a static member...Java Code:private static <datatype> <indentifier> = initializeMyStaticObject();
- 01-29-2010, 04:44 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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).
- 01-29-2010, 05:00 PM #9
Member
- Join Date
- Dec 2009
- Posts
- 68
- Rep Power
- 0
Similar Threads
-
Static Builder and Create Methods for simple class
By Pilot Ace in forum New To JavaReplies: 3Last Post: 08-30-2009, 10:18 AM -
Debuggin help - simple program non-static method errors
By RR_QQ in forum New To JavaReplies: 5Last Post: 02-04-2009, 01:20 AM -
[SOLVED] static block or constructor?
By JT4NK3D in forum New To JavaReplies: 3Last Post: 05-27-2008, 02:21 PM -
Demonstration of a simple constructor
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:47 PM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks