aspose file tools*
The moose likes Java in General and the fly likes Do we have anything in Java like we have typedef in C++ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Do we have anything in Java like we have typedef in C++" Watch "Do we have anything in Java like we have typedef in C++" New topic
Author

Do we have anything in Java like we have typedef in C++

Harish V Gupta
Ranch Hand

Joined: Dec 17, 2011
Posts: 33
I was wondering if we have any way to declare a variable with our own name. For example,


Please don't say to extend the class and then declare because that won't be same (for example, private variables will be missing).
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

Harish V Gupta wrote:I was wondering if we have any way to declare a variable with our own name. For example,


Please don't say to extend the class and then declare because that won't be same (for example, private variables will be missing).


No. I don't think that there is an equivelant of typedef in Java.

Sorry,
Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36486
    
  16
If I remember correctly, typedef actually comes from C. You don’t need it in a language where you can create classes.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Is this just a general enquiry or do you think you have a use for such a thing ?
If the latter it might be better to explain what you are trying to do and then maybe someone will be able to suggest a solution.

Joanne
Harish V Gupta
Ranch Hand

Joined: Dec 17, 2011
Posts: 33
I do need to use it. I am actually reading a file which can be of either xls or xlsx format. We have one API each for rendering xls and xlsx file. For xls it's HSSFWorkbook and for xlsx it's XSSFWorkbook. Both these classes have methods with same names in it. So if I can have a synonym type, I can use it to declare either HSSFWorkbook or XSSFWorkbook variable as the case may be.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

The way you would usually do that in Java would be to have an interface that defines the operations, and two classes that implement the interface. You then declare the variable using the interface type.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2052
    
  22

In this case, both HSSFWorkbook and XSSFWorkbook implement the Workbook interface. You should really be coding to the Workbook interface instead of the implementation classes.
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 865
    
    5
Harish V Gupta wrote:
Please don't say to extend the class


The class String is final anyway.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7054
    
  16

Harish V Gupta wrote:I do need to use it. I am actually reading a file which can be of either xls or xlsx format. We have one API each for rendering xls and xlsx file. For xls it's HSSFWorkbook and for xlsx it's XSSFWorkbook. Both these classes have methods with same names in it. So if I can have a synonym type, I can use it to declare either HSSFWorkbook or XSSFWorkbook variable as the case may be.

Seems to me like a basic class hierarchy rather than a typedef, even in C++; but it has been quite a while. In Java, if it was me, I'd probably base it around a WorkBook interface, as already mentioned.

Another possibility, of course, is an enum.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Do we have anything in Java like we have typedef in C++
 
Similar Threads
Understanding String as an Object
Creating our own String class?
Why mutable elements of Sets?
How to best get value of int variable into another method?
Why are Wrapper Classes, String,... final ?