Re: static member question




"cdx" <jm1@xxxxxxxxxxxx> schrieb im Newsbeitrag
news:ccKdnTpPhPmKgwjfRVn-pQ@xxxxxxxxxxxxxx
> I'm not sure if java has this capability, but can someone let me know?
>
> Suppose I have Class A, with subclasses A1 and A2
>
>
> I want a static variable with the same name for each of the subclasses.
> For instance, I would like to have this heirarchy have a static var called
> "factory" such that each subclass will have it's own single instance of
> it. I know that I can define this static var at each of the subclass
> levels (Class A1 and A2), but is there a way to define it in Class A such
> that A1 and A2 will have their own? I thought I remember a special type
> like this in C#, but could not find anything like this in java.

No, you cannot do that. You have to declare a static variable in each of the
classes if you want each of them to have its own.

In Java you cannot have a baseclass with static elements (like a static
factory method or a static getInstance() method of a singleton) which is
extended by a concrete subclass (e.g. a concrete Singleton) which then has
these static elements of the baseclass for its own.

If you just want to implement the abstract factory design pattern, only your
abstract baseclass (A) needs the factory field (an instance of the concrete
subclass A1 or A2). Your clients will always use A.getFactory() to get the
right implementation and do not know anything about A1 or A2.

Regards
Sebastian


.



Relevant Pages

  • Re: operator overloading
    ... Multipliable, Ring extends Addable, Multipliable, Dividable, Field extends ... There is a class of types, Num, which represent ... little in the Java SE library that I would opt to remove. ... and you subclass BigDecimal as ...
    (comp.lang.java.programmer)
  • Re: which foo(x)
    ... You did a good analysis of the situation of Java. ... The way the code to execute for this call is like this: ... that this class is either C, or a subclass of C. ... called "single dispatch" because they dispatch (choose the method to ...
    (comp.lang.java)
  • Re: [ANN] jfli - a Java FLI for LispWorks
    ... Based on what you write there, I managed to run it with the Java SDK - see my ... >> in using Java libraries, the ability to create instances of Java classes and ... > Well, a nice library, such as SWT/Eclipse, often offers interfaces. ... Yes, although even in those cases it is sometimes more convenient to subclass, ...
    (comp.lang.lisp)
  • Re: Can anybody please check this code out?
    ... I'm rather inexperienced in Java but I'm in need of defining a subclass ... public boolean isCellEditable(int rowIndex, int columnIndex) ... so that a cell must be editable according to the superclass, ...
    (comp.lang.java.programmer)
  • Re: how to override isCellEditable in MATLAB
    ... I have never used MATLAB's Java interface, so I may be wrong here. ... creating a subclass of JTable and using that. ... create an instance of that from MATLAB instead of a JTable. ... I suggest you look into using a custom TableModel instead of subclassing ...
    (comp.lang.java.programmer)