1

Is it possible to access the private static method within the object creation scope? Something like the below snippet.

class A {
   private static void func(int x) {
      /*Some Code here*/
   }
}

and call from another context, something like this :

new A(){{
  func(2);
}};

The intent here is to facilitate for addition of more calls to func(int), with a differnt argument for each call, or to put simpler, some sort of builder for the object of type A.

Something like this :

new A() { {
    func(2);
}
{
    func(3);
}
// ...
};

Is this possible to achieve ?

Perception
  • 79,279
  • 19
  • 185
  • 195
user2109934
  • 31
  • 1
  • 6
  • You mean call methods from static blocks ? Or inline initialize an object with something similar to what's possible with ArrayLists ? http://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line – James P. Feb 26 '13 at 06:01
  • Btw, builder is also a specific design pattern. It may be misleading to readers. http://stackoverflow.com/questions/328496/when-would-you-use-the-builder-pattern – James P. Feb 26 '13 at 06:04

2 Answers2

1

I think it is something you can find out the answer easily by actually writing some simple piece of code and try it out yourself.

From my understanding (haven't tried) it is not possible.

I think you have to understand what is the meaning of:

new A() { { func(2); } }

This is anonymous inner class which is just like writing something like

class A_anonymous extends A {
  { 
    func(2); 
  }
}

and do a

new A_anonymous();

Further look into the class definition, it is in fact translated to

class A_anonymous extends A {
  public A_anonymous() { 
    func(2); 
  }
}

So the answer is obvious. If func() is a private method in A, then of course you cannot use it such way.

The way to solve is simply make the visibility of the method to be protected (or higher).

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • @Perception Slightly OT. Out of curiosity, dou you reckon there is a way to get around not being able to call methods from a static context ? Like some kind of staticly-referred singleton registry where each new object created is added. That coupled with some interface-level methods. – James P. Feb 26 '13 at 06:09
  • 1
    @JamesPoulson - that sounds like a complicated way to implement a factory. – Perception Feb 26 '13 at 06:15
  • Yes. But then I'm a complicated person :p . Just exploring possibilities. – James P. Feb 26 '13 at 06:24
  • it seems to me things you want to do is like those Fluent Factory (something like EqualsBuilder or ToStringBuidler in Apache Commons Lang). – Adrian Shum Feb 26 '13 at 08:32
  • @Adrian Shum Thank you for your answer.Broadening the access specifier of the method seems to be the only way. – user2109934 Feb 26 '13 at 11:21
0

Is it possible to access the private static method within the object creation scope?

Yes.

Within constructor you can call a static method.

class AClass
{
    private void static aMethod1()
    {

    }

    private void static aMethod2()
    {

    }

    public AClass()
    {
        aMethod1();
        aMethod2();
    }
}
Azodious
  • 13,752
  • 1
  • 36
  • 71