0

I have a situation where i have to do like

 class A{

    B b;

    class B extends ContainerView{

    public B(int x,int y, String s1, Styring s2){
    super(x,y,200,500);

    }
    }

    class C{

    void m(){
    if(...){
    B b=new B(x,y,raj,esh);
    b.something....
    else
    B b=new B(x,y,esh,raj);

The problem is x and y is unknown to the class C and if i declare it as int x,y in class C then it's behaving improperly....

What to do? Thanks in Advance

Rajesh Kumar
  • 363
  • 2
  • 5
  • 13

2 Answers2

0

Take a look on Java inner class and static nested class

class OuterClass {
    ...
    class InnerClass {
        ...
    }
}

OuterClass.InnerClass innerObject = outerObject.new InnerClass();

Community
  • 1
  • 1
Ran Adler
  • 3,587
  • 30
  • 27
0

The problem here is your usage of the class or the class constructor API is wrong.

You gave two options when calling public B(int x,int y, String s1, String s2) and x and y is unknown.

First option is to pass null or or a default value there (since in this case the parameters are int the default can be 0 or something like -1). The class you are creating should be aware that these values may be null or contain an invalid/default value. This is however a patch.

If properly designed and x and y parameters are optional it should contain a constructor to reflect that public B(String s1, String s2)

The second option is to not create that particular object there, if x and y is unknown and those parameters are no optional. You have placed it in the wrong place where critical contextual parameters for object creation is not present.

Thihara
  • 7,031
  • 2
  • 29
  • 56