• 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

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

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I remember correctly, typedef actually comes from C. You don’t need it in a language where you can create classes.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Harish V Gupta
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harish V Gupta wrote:
Please don't say to extend the class



The class String is final anyway.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic