What is the best constructor? Empty or using fields?

I am currently taking the Android class, and when I received the first assignment class, I lost points due to using an empty constructor, and not using one with fields. The professor said using fields is better. He is right? If so, why?

This is better?

Photos pic = new Photos(path, title, author, date);

public Photos(String url_path, String title, String author,
        String date_taken) {
    super();

    this.url_path = url_path;
    this.title = title;
    this.author = author;
    this.date_taken = date_taken;
} 

Or that? Or does it matter?

 Photos pic = new Photos();
 pic.setUrl_path(path);
 pic.setTitle(title);
 pic.setAuthor(author);
 pic.setDate_taken(date);

 public Photos() {
    super();

 }
+4
source share
8 answers

In general, you should prefer a constructor that is given initial values ​​and set these values ​​later using setter methods. But why?

, , . no-args, setter, .

( 1970- ), .

, Person, , , String, , . , null. no-args, ? . , . , , Person ( null), , , Person.

, , , . , , . , , , , ( ), . , , . , .

+1

"", . "" , . , :

Photos pic = new Photos();
pic.setUrl_path(path);
pic.setTitle(title);
pic.setAuthor(author);
pic.setDate_taken(date);

pic ? - Photos? "", , Photos, .

, . , , , . - , .

, Photos, . , , :

public Photos() {
    // set default values
}

public Photos(String url_path, String title, String author, String date_taken) {
    // set supplied values
}

, Photos ( ) .

+4

setter, , , , .

, . .

:

public class Setters {
   private int value;
   public void setValue(int val) {
     value=val;
   }
   public int getValue() {
     return value;
   }
}

public void function(Setter setter){
    setter.setValue(20);
}


// In main 

Setter setter = new Setter();
setter.setValue(10);
function(setter);
System.out.println(setter.getValue());

20,

public class Setters {
   private int value;
   public setter(int val) {
     value=val;
   }
   public int getValue() {
     return value;
   }
}

// In main 

Setter setter = new Setter(10);
Setter setter1 = new Setter(20);
System.out.println(setter.getValue());
System.out.println(setter1.getValue());

10 20, , .

+2

, . construction object " > strong > .

.

File,

File f = new File("filepath");

filepath - , File , , . , readable, .

f.setReadable(arg0)

Photo - , parameterized .

+2

, . Immutable, , /. , . .

, Title, Date_Taken Author, "" , . getter/setter URL-, .

+1

, , .

:

JButton b = new JButton();
b.setText("Hello everybody 1");
b.setIcon(b1);

JButton c = new JButton();
c.setText("Hello everybody 2");
c.setIcon(c2);

JButton d = new JButton();
d.setText("Hello everybody 3");
d.setIcon(d3);

JButton e = new JButton();
e.setText("Hello everybody 4");
e.setIcon(e4);

JButton b = new JButton("Hello everybody 1", b1);

JButton c = new JButton("Hello everybody 2", c2);

JButton d = new JButton("Hello everybody 3", d3);

JButton e = new JButton("Hello everybody 4", e4);

, ? , , , , getgo, , , .

+1

Photo ( ) , , . (, , , );

. Photo (not Photos)

public Photo(String urlPath, String title, String author,
    String dateTaken) {
super();

this.urlPath= urlPath;
this.title = title;
this.author = author;
this.dateTaken= dateTaken;

}

:

super(), Photo . .

+1

, . , spring guice, .

, , , , .

+1

All Articles