0

I have written the below java class

public class FileAttachment  implements java.io.Serializable {
private java.lang.String fileName;

private java.lang.String fileExt;

public FileAttachment() {
}

public FileAttachment(
       java.lang.String fileName,
       java.lang.String fileExt) {
       this.fileName = fileName;
       this.fileExt = fileExt;
}


/**
 * Gets the fileName value for this FileAttachment.
 * 
 * @return fileName
 */
public java.lang.String getFileName() {
    return fileName;
}


/**
 * Sets the fileName value for this FileAttachment.
 * 
 * @param fileName
 */
public void setFileName(java.lang.String fileName) {
    this.fileName = fileName;
}


/**
 * Gets the fileExt value for this FileAttachment.
 * 
 * @return fileExt
 */
public java.lang.String getFileExt() {
    return fileExt;
}


/**
 * Sets the fileExt value for this FileAttachment.
 * 
 * @param fileExt
 */
public void setFileExt(java.lang.String fileExt) {
    this.fileExt = fileExt;
}
}

If I try to initialize the class as an array as shown below . What value I need to assign to it's variable fileattachments . I know I can assign null value to it. But, I want to assign anything other than null.

  FileAttachment[] fileattachments = //what non null value can I assign it ?
  • You can assign a `FileAttachment[]` to it, because that's what it is. – Christopher Schneider Feb 13 '17 at 18:53
  • 1
    [How to initialize an array in Java?](http://stackoverflow.com/q/1938101/669576) or [How to initialize an array of objects in Java](http://stackoverflow.com/q/5889034/669576). – 001 Feb 13 '17 at 18:54

2 Answers2

3

Well you could assign a new array, or you could use an initializer to add some elements to the array.

// non-null but empty array
 FileAttachment[] fileattachments = new FileAttachment[10];
// initializer with one element
 FileAttachment[] fileattachments = { new  FileAttachment( "myName", "txt" ) };

For completeness Andy pointed out that a method invocation would also work. You typically want a static method for this sort of thing, although if you're very careful an instance method could work.

FileAttachment[] fileattachments = Utils.genAttachments();

and...

public class Utils {
    public static FileAttachment[] genAttachments() {
        FileAttachment[] retVal = new FileAttachment[100];
        for( int i = 0; i < retVal.length; i++ )
            retVal[i] = new FileAttachment( "MyDoc"+i, "doc" );
        return retVal;
     }
}
markspace
  • 10,621
  • 3
  • 25
  • 39
  • Or `null`, of course. Or the result of a method invocation returning a `FileAttachment[]`. – Andy Turner Feb 13 '17 at 19:05
  • The OP did say "besides null." Besides a method invocation I also could use an explicit initializer block, but my gut feel here is the OP wants something lighter weight. (I might let the OP chime in whether this fixes his problem or if the OP wants a more extensive answer.) – markspace Feb 13 '17 at 19:25
  • "You typically want a static method for this sort of thing" Why so? – Andy Turner Feb 13 '17 at 19:33
  • Because the class won't be fully initialized when an instance method is invoked, which could yield surprising results. – markspace Feb 13 '17 at 19:34
2

I would use an ArrayList instead of an Array because you cannot easily resize an Array if it becomes too small for your container.

FileAttachment[] fileAttachments = new FileAttachment[5]

The above code will allow you to put 5 attachments into your Array, but no more, unless you reinitialize it as a new FileAttachment[10]

Using an ArrayList, you can just infinitely add things. See the following code:

List<FileAttachment> fileAttachments = new ArrayList<FileAttachment>();

FileAttachment attachment = new FileAttachment("myFileName", "txt");
fileAttachments.add(attachment);

attachment = new FileAttachment("myOtherFileName", "xls");
fileAttachments.add(attachment):
jseashell
  • 745
  • 9
  • 19