Public vs private instance fields

From: Brian Munroe (brian.e.munroe_at_gmail.com)
Date: 03/29/05


Date: 29 Mar 2005 11:19:22 -0800

Newbie Java/OOP question here, which is more correct?

1) Define a public instance field and access it like:

Object o = new Object()
o.field1 = "hello";
System.out.println(o.field1);

2) Define a private instance field and use set/get methods to access
it:

Object o = new Object()
o.setField1("hello");
System.out.println(o.getField1());

I prefer to make it public, less typing all around, but being my luck,
it isn't the correct way either :)

thanks

-- brian



Relevant Pages

  • Re: Public vs private instance fields
    ... >1) Define a public instance field and access it like: ... >2) Define a private instance field and use set/get methods to access ... using accessors and mutators (get/set methods) is ... Plus, as for typing, any decent Java IDE should generate the get/set ...
    (comp.lang.java.help)
  • Re: I finally did it-- hungarian notation
    ... MS suggest camel for Protected instance field ... and Pascal for Public instance field ... Do you think they meant Friend, Private and Protected as protected ...
    (microsoft.public.dotnet.languages.vb)