Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By JosAH

Thread: initialize and reinitialize a static field

  1. #1
    ghandil is offline Member
    Join Date
    Jun 2013
    Posts
    28
    Rep Power
    0

    Default initialize and reinitialize a static field

    Could you please explain Why should it be private and even static?

    A private static method can be used to initialize and reinitialize a static field
    Thanks

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: initialize and reinitialize a static field

    Please give the source of the quote you posted.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: initialize and reinitialize a static field

    Any non-private, non-static method can do the same ...

    kind regards,

    Jos
    ghandil likes this.
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: initialize and reinitialize a static field

    The main reason I can think of is that it is to serve as an internal flag. For example, the default size of
    an internal array. It is private because it is not necessary for anyone to see it or access it. It is static
    because it applies to all instances of the class.

    Doing something like this:
    Java Code:
    private static int DEFAULT_SIZE = 20;
    int [] array = new int[DEFAULT_SIZE];
    is preferable to:
    Java Code:
    int[] array = new int[20];
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: initialize and reinitialize a static field

    I normally do it like this (especially for the maintenance programmers):

    Java Code:
    	private static int l(int v) { v-=(v>>1)&0x55555555;v=(v&0x33333333)+((v>>2)&0x33333333); return ((v+(v>>4)&0xF0F0F0F)*0x1010101)>>24; }
    	private static int[] a= new int[l(1048575)];
    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    ghandil is offline Member
    Join Date
    Jun 2013
    Posts
    28
    Rep Power
    0

    Default Re: initialize and reinitialize a static field

    Quote Originally Posted by Norm View Post
    Please give the source of the quote you posted.
    It's not an official source: Level 10 - basic java, - Memrise
    Quote Originally Posted by JosAH View Post
    Any non-private, non-static method can do the same ...
    I've thought of it as a setter method. Not sure how accurate it is.
    Quote Originally Posted by jim829 View Post
    The main reason I can think of is that it is to serve as an internal flag. For example, the default size of
    an internal array. It is private because it is not necessary for anyone to see it or access it. It is static
    because it applies to all instances of the class.

    Doing something like this:
    Java Code:
    private static int DEFAULT_SIZE = 20;
    int [] array = new int[DEFAULT_SIZE];
    is preferable to:
    Java Code:
    int[] array = new int[20];
    Hmm, Method, flag, ...

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: initialize and reinitialize a static field

    What a nice guy. You can also transliterate all the non-keyword tokens to meaningless strings too.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: initialize and reinitialize a static field

    Not convinced by that site at all.
    eg:
    static void
    what method "main" modifiers never on constructors

    Sorry, but that is not limited at all to the main method, just as a private static method has nothing to do with initialising things.

    It's like it's been written by people who haven't actually worked with Java and have only seen a YouTube tutorial that they didn't really understand.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 6
    Last Post: 06-30-2015, 06:19 PM
  2. Replies: 11
    Last Post: 04-11-2012, 05:57 PM
  3. How to Declare and Initialize static array
    By parulmahajan in forum New To Java
    Replies: 6
    Last Post: 08-05-2011, 07:48 AM
  4. How can I share a static field between 2 class loaders?
    By zibilico in forum Advanced Java
    Replies: 6
    Last Post: 03-09-2010, 01:31 PM
  5. static final field
    By techie.it19 in forum New To Java
    Replies: 3
    Last Post: 10-16-2008, 04:12 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
  •