5

I'm quite base in Java because I'm full time C# developer but i need make some private project in Android.

In C# i can do something like this :

public class Test
{
    public string A {get;set;}
    public OtherClass B {get;set;}
}

public OtherClass ()
{
    string Some {get;set;}
    string Again {get;set;}
}

Now when I want to use class test i can do code like:

var test = new Test
{
    A = "test",
    B = new OtherClass
    {
        Some = "Some",
        Again = "Again"
    }
};

Thank to this now i have initialized test in clear way.

When i want do this in Java i must make code like :

public class Test
{
    private string A;
    public string GetA()
    {
        return A;
    }
    public void SetA(string a)
    {
        A = a;
    }

    privte  OtherClass B;
    public OtherClass GetB()
    {
        return B;
    }
    public void SetB(OtherClass b)
    {
        B = b;
    }
}



public class OtherClass
{
    private string Some;
    public string GetSome()
    {
        return Some;
    }
    public void SetSome(string some)
    {
        Some = some;
    }

    privte  string Again;
    public string GetAgain()
    {
        return Again;
    }
    public void SetAgain(string again)
    {
        Again = again;
    }
}

I know that Java must have Seter and Geter to field and i'm ok with this but now if i want to use Test object in way like i use it in C# i must do something like :

OtherClass otherClass = new OtherClass();
otherClass.SetSome("Some");
otherClass.SetAnothier("Another");
Test test = new Test();
test.SetA("A")
test.SetB(otherClass);

IMO this is not nice and clear declaration. I know that i can add constructor like :

public Test(string a, otherClass b)
{
    A = a;
    B = b;
}

and

public OtherClass(string some,string another)
{
     Some = some;
     Another = another;
}

And use it like :

Test test = new Test("Test",new OtherClass("some","Another"));

But when i have more complicated classes ( e.g with 200 field ( yes i have that )) the constructor will be very long and it will hard to read it.

So can I initialize class in Java in other way that this I show you ?

Thank

Aht
  • 583
  • 4
  • 25

1 Answers1

10

You can actually use object initializers in java also, however they are called double brace initializers. Example:

Test test = new Test()
{{
    SetA("test");
    SetB(new OtherClass()
    {{
        SetSome("Some");
        SetAgain("Again");
    }});
}};

However you should probably not use this since it has some weird/unexpected side effects. This example produces an AnonymousInnerClass for Test and OtherClass. You can read more about double brace initializers here.

If you have more parameters than you wish to pass down your constructor you could use the builder pattern as suggested by this answer.

Community
  • 1
  • 1
Simon Karlsson
  • 4,090
  • 22
  • 39
  • It look like that was I looking for. Those double brace scare me a little bit because amount of brace can make find bug even worse especial when will initialize object in some function :) But this look perfect to me. It is taking less resource than method hat i show in my post ? – Aht Feb 05 '16 at 10:13
  • 1
    It takes much more resource - every object you create like this has its own class. – Pete Kirkham Feb 05 '16 at 10:16
  • 1
    That's actually a misuse of anonymous inner classes - made popular by people who really really want Java to look like C#. – Dave Doknjas Feb 05 '16 at 13:52
  • 1
    @Aht: The main concern should not be a cosmetic concern - your main concern should be "is the code doing what needs to be done" - in this case there's this hidden class instantiation going on that has no business happening here. – Dave Doknjas Feb 06 '16 at 01:46
  • @DaveDoknjas: So i will remember that solution but i will try to avoid it – Aht Feb 06 '16 at 12:45