Results 1 to 6 of 6
  1. #1
    ferfykins is offline Member
    Join Date
    Feb 2017
    Posts
    23
    Rep Power
    0

    Default question about static

    Java Code:
    private static StockList stockList = new StockList();

    ^wondering what the static part does for this.... as it's an object instance of StockList class

    I know what static methods and variables do, but i've never heard of a static instance of class

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: question about static

    The keyword static makes the instance the same for each instance of the enclosing class.

    Example 1: static
    Java Code:
    public class Bla {
       private static StockList stockList = new StockList();
    }
    Example 2: non-static
    Java Code:
    public class Bla {
       private StockList stockList = new StockList();
    }
    In the first example, every instance of Bla has the *same* instance of StockList. In the second example, every instance of Bla has *its own* instance of StockList.

    The Singleton pattern makes use of this quite often (Unless you're doing the enum-singleton as promoted by Joshua Bloch). A very naive implementation of a Singleton looks like this:
    Java Code:
    public class StockList {
       private static StockList stockList = new StockList();
    
       public static StockList getInstance() {
           return stockList;
       }
    }
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

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

    Default Re: question about static

    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: question about static

    Mandatory read: Be Forthright When Cross Posting To Other Sites
    https://www.coderanch.com/wiki/66034...-Posting-Sites
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    ferfykins is offline Member
    Join Date
    Feb 2017
    Posts
    23
    Rep Power
    0

    Default Re: question about static

    Thanks guys,i think i get it now... i post multiple sites to get multiple answers, makes understanding easier from multiple perspectives...Thanks.

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,989
    Rep Power
    8

    Default Re: question about static

    I am glad you learned a few things about static variables.

    I understand why you post to multiple sites, and it sounds logical at first to spread the question to as many sites as possible to get as many answers. I also hope you understand my frustration when I have just typed a post, examples and all, only to find out other people have done the same.

    If you crosspost ALWAYS provide the links to the other posts, so I don't waste my valuable time on typing double answers.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. A question about the term 'static'
    By zFollette in forum New To Java
    Replies: 4
    Last Post: 12-23-2013, 04:05 PM
  2. question about static methods...
    By ajetrumpet in forum New To Java
    Replies: 10
    Last Post: 04-24-2013, 01:19 PM
  3. static question
    By stuckonjava in forum New To Java
    Replies: 3
    Last Post: 05-10-2012, 09:39 PM
  4. Static and non static class methods question
    By silverglade in forum New To Java
    Replies: 2
    Last Post: 05-14-2011, 10:10 PM
  5. Newbie question about Static methods
    By SCS17 in forum New To Java
    Replies: 9
    Last Post: 02-06-2008, 08:03 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
  •