• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Questions about java bean

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all!

I am having two questions, kindly answer them.

Q1: Can we have data members with default access modifier in a java Bean?

Q2: Is it must to provide getter and setter methods for all the properties of a java bean or we can skip one if we don't need it?
 
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Adnan,

Surely we can have data member with default access / public access in the Java Bean. But it's good practice to have data member as Private and get them access by getter and setter methods.

also you need not to make getter and setter method for each of the data member. But if you dont have a getter /setter method for a private data member you won't be able to access that data member.
 
Marshal
Posts: 72472
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The idea of a bean is that it has a no-arguments public constructor and public get and set methods for every field. That way it is possible to write the state of the class into an XML file and reconstruct it later.
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic