wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes static class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static class" Watch "static class" New topic
Author

static class

Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

If below syntax can work then what's problem with my program ?

Locale aLocale = new Locale.Builder().setLanguage("fr").setRegion("CA").build();


---------- Compile ----------
MyOuter.java:9: error: incompatible types
MyOuter obj=new MyOuter.MyInner();
^
required: MyOuter
found: MyInner
1 error

Output completed (0 sec consumed) - Normal Termination


OCA7
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4371
    
    8

MyOuter.MyInner is a completely different class to MyOuter, it just happens to be declared within it. It doesn't work for exactly the same reason as you can't assign a JButton to a String variable.
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

Then how is this syntax working ?
Locale aLocale = new Locale.Builder().setLanguage("fr").setRegion("CA").build();
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4371
    
    8

Because you're making further method calls. Most of the methods of a Locale.Builder object return the same Locale.Builder object - that's a common approach to allow the chaining together of calls like this. But the important bit of that line is the last call to build(). That call (see java.util.Locale.Builder#build()) returns a Locale object.

Writing that line out longhand would look like this:

Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

Thanks.It works.

---------- Run ----------
Output completed (0 sec consumed) - Normal Termination
Anand Damodaran
Greenhorn

Joined: Oct 04, 2012
Posts: 21
Hi,
Add the following line,

MyOuter.Inner s= new MyOuter.Inner();
Enkita mody
Ranch Hand

Joined: Aug 06, 2012
Posts: 333

Anand Damodaran wrote:Hi,
Add the following line,

MyOuter.Inner s= new MyOuter.Inner();


That was not real problem.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static class