• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

When a Method's return type is Class Name.StaticClass

 
Ranch Hand
Posts: 333
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I ran across something today at work that I haven't seen before ( still new to Java ) and when I Google for examples or tutorials I'm not finding anything.  

A method where the return type is ClassName.StaticClass?    In the example below Api is the class and Builder is the static class.  
The static class method has a variable name "messageType"  with type class MessageType.  

If you can help, I have two questions please.  
1) Since I know if that if the return type of a method is String or int for example then the return type would be a String or an int so I assume the return type of the setMessageType method is what is in the static class Builder?


2) Would the setMessageType method's parameter of MessageType, only use the MessageType class which the static class is using?    

What I am trying to say is there are two classes named "MessageType" which I have the option to use.    The class Api is contained in a jar file that I had to import into my project.  The project already has a class named MessageType and the jar file has a class name MessageType.

I tried to pass the setMessageType method a MessageType argument using the class that was in my project instead of what was in the jar file and it wouldn't accept it.    So I assume it's because of the return type.  


Thank You!



 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lisa,

A static class is just like any other top level class, except it uses the enclosing class' name as part of its fully qualified name. Depending on the context and your import statements, you can refer to it as either Builder or Api.Builder. Static classes are useful for two purposes:

1) They can access private members of the enclosing class. Useful if the classes are tightly coupled.
2) They can be differentiated from other classes with the same name, without having to fully qualify them. For instance, the difference between Rainbow.Color and ChessPiece.Color is clear.

They don't affect any other part of the code. If the return type of setMessageType() had been a class Builder from another part of the source code, it wouldn't matter for how Java interprets the MessageType parameter.

Java determines the fully qualified name of the type of the method parameter like it does for any other simple type identifier: If you have a class with that name in the same package as your current code, Java uses that one. Otherwise, Java looks at your import statements. If you want to use MessageType from one or the other library, you have to use an import statement that gives the fully qualified name of the type you want to use. Presumably they are from different packages.
 
Lisa Austin
Ranch Hand
Posts: 333
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Hi Lisa,

A static class is just like any other top level class, except it uses the enclosing class' name as part of its fully qualified name. Depending on the context and your import statements, you can refer to it as either Builder or Api.Builder. Static classes are useful for two purposes:

1) They can access private members of the enclosing class. Useful if the classes are tightly coupled.
2) They can be differentiated from other classes with the same name, without having to fully qualify them. For instance, the difference between Rainbow.Color and ChessPiece.Color is clear.

They don't affect any other part of the code. If the return type of setMessageType() had been a class Builder from another part of the source code, it wouldn't matter for how Java interprets the MessageType parameter.

Java determines the fully qualified name of the type of the method parameter like it does for any other simple type identifier: If you have a class with that name in the same package as your current code, Java uses that one. Otherwise, Java looks at your import statements. If you want to use MessageType from one or the other library, you have to use an import statement that gives the fully qualified name of the type you want to use. Presumably they are from different packages.



Oh!  Okay , Thank You very much for the explanation.  
 
reply
    Bookmark Topic Watch Topic
  • New Topic