9 Replies - 322 Views - Last Post: 15 July 2019 - 06:28 PM Rate Topic: -----

#1 ferfykins   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 69
  • Joined: 05-August 15

Question about static

Posted 14 July 2019 - 07:43 PM

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
Is This A Good Question/Topic? 0
  • +

Replies To: Question about static

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12634
  • View blog
  • Posts: 45,800
  • Joined: 27-December 08

Re: Question about static

Posted 14 July 2019 - 07:48 PM

Quote

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


You have a static variable- nothing more, nothing less.
Was This Post Helpful? 0
  • +
  • -

#3 ferfykins   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 69
  • Joined: 05-August 15

Re: Question about static

Posted 14 July 2019 - 07:50 PM

View Postmacosxnerd101, on 14 July 2019 - 07:48 PM, said:

Quote

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


You have a static variable- nothing more, nothing less.



I'm confused, it looks like how you create an object of a class?
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12634
  • View blog
  • Posts: 45,800
  • Joined: 27-December 08

Re: Question about static

Posted 14 July 2019 - 07:53 PM

The static variable happens to be an Object. Note that the static has no bearing on the variable stockList's internal interactions. Rather, the static modifier deals with the class managing the variable stockList.
Was This Post Helpful? 0
  • +
  • -

#5 ferfykins   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 69
  • Joined: 05-August 15

Re: Question about static

Posted 14 July 2019 - 07:55 PM

View Postmacosxnerd101, on 14 July 2019 - 07:53 PM, said:

The static variable happens to be an Object. Note that the static has no bearing on the variable stockList's internal interactions. Rather, the static modifier deals with the class managing the variable stockList.




i know what a static variable of a class does, it makes it so all objects with that variable share the same value
but what does this do, static for an object of a class?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12634
  • View blog
  • Posts: 45,800
  • Joined: 27-December 08

Re: Question about static

Posted 14 July 2019 - 08:18 PM

View Postferfykins, on 14 July 2019 - 10:55 PM, said:

i know what a static variable of a class does


As I explained in my first post, you have a static variable. There is nothing special about the fact that the variable is an Object rather than a primitive.
Was This Post Helpful? 0
  • +
  • -

#7 ferfykins   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 69
  • Joined: 05-August 15

Re: Question about static

Posted 14 July 2019 - 08:22 PM

View Postmacosxnerd101, on 14 July 2019 - 08:18 PM, said:

View Postferfykins, on 14 July 2019 - 10:55 PM, said:

i know what a static variable of a class does


As I explained in my first post, you have a static variable. There is nothing special about the fact that the variable is an Object rather than a primitive.



Does that mean all varialbes of stocklist are the same value? Since its' a objet it has multiple field values
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12634
  • View blog
  • Posts: 45,800
  • Joined: 27-December 08

Re: Question about static

Posted 14 July 2019 - 08:37 PM

There is no need to quote the post above yours. Please use the Reply button at the end of the thread.

To answer your question, no. The static modifier means that the variable stockList is associated with the class/blueprint/recipe, rather than the instance of whatever class is storing the stockList variable. Note that this is what the static modifier means for primitive-type variables as well...
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11604
  • View blog
  • Posts: 19,726
  • Joined: 19-March 11

Re: Question about static

Posted 15 July 2019 - 07:50 AM

I don't know the name of the class that this line appears in, but for the sake of argument let's suppose it's an Inventory class. Feel free to globally replace "Inventory" with the real name of the class, as you read this, if it helps with clarity.

What this line means is that there is a single instance of StockList that all instance of the class Inventory have access to. If this were not static, then each instance of Inventory would have a distinct StockList.
Let's suppose you have two Inventory instances at some moment in time, partsInventory and widgetsInventory. Since lists tend to be mutable, this means that when partsInventory adds something to its stockList, widgetsInventory's stockList will also change (and vice versa). This can be a tempting design decision (hooray, everyone's got the same list!) but in practice it often leads to odd bugs and can make software difficult to understand and to test.

In general, static methods should (ironically, given the name) not be stateful - that is, they should not depend on or change the state of the classes they relate to. In this case, as I say, the stockList is likely to be stateful, so this is a questionable design decision.
Was This Post Helpful? 1
  • +
  • -

#10 ferfykins   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 69
  • Joined: 05-August 15

Re: Question about static

Posted 15 July 2019 - 06:28 PM

Thanks Jon, i think i get it now :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1