By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,799 Members | 1,403 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,799 IT Pros & Developers. It's quick & easy.

Converting VB to Java - 'type' convert for record

P: 2
I have zero Java knowledge, what would be the equivalent of the following in java?

Expand|Select|Wrap|Line Numbers
  1. Public Type hdrmail1rec                             'Output rec header 1
  2.     hdrcust As String * 20
  3.     filler1 As String * 3
  4.     hdrpo As String * 20
  5.     filler2 As String * 3
  6.     hdrdate As String * 23
  7.     filler3 As String * 3
  8.     hdrstat As String * 40
  9. End Type
Jun 3 '13 #1
Share this Question
Share on Google+
3 Replies


Nepomuk
Expert 2.5K+
P: 3,112
Hi Jack and welcome to bytes.com!

Java doesn't have an own type for records and Strings aren't fixed length either. The closest to the above would be a Bean with String members or, if you insist on fixed lengths, char arrays. A bean is a very basic Java class with private variables as well as getter and setter functions for those variables. Of course, you could enforce a certain length in those setter functions.
Here's a truncated example:
Expand|Select|Wrap|Line Numbers
  1. public class hdrmail1rec {
  2.    private String hdrcust;
  3.    // ...
  4.  
  5.    public String getHdrcust() {
  6.       return hdrcust;
  7.    }
  8.  
  9.    public void setHdrcust(String hdrcust) {
  10.       if(hdrcust.length() > 20) {
  11.          this.hdrcust = hdrcust.substring(0,21);
  12.       } else {
  13.          this.hdrcust = hdrcust;
  14.       }
  15.    }
  16.    // ...
  17. }
In theory, you could make the String variables public and do without the getters and setters; this is however considered bad practice due to security reasons (as any class could then access the variables without you having any control over it). Also, many libraries and frameworks follow the convention of having getters and setters.
Jun 4 '13 #2

P: 2
thanks for the info nepomuk! I am stumbling through converting a program to Java, kind of as a learning experience and it's also something that would help out at work.

I appreciate the info!

-jack25
Jun 4 '13 #3

Nepomuk
Expert 2.5K+
P: 3,112
You're welcome. Java uses a few concepts that are probably quite different from VB (though just guessing, as I never seriously developed in VB myself) so just converting it probably won't be the best method if efficiency is essential. As a learning experience of course, it can be very useful.
Jun 5 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.