Sorry for asking some of the fundamental concept about java.
I have confusion about how to access private fields within a class.
for example, i have defined a class:
private class Example{
private Timestamp fromdate;
private Timestamp todate;
private float amount;
public example(Timestamp fromdate, Timestamp todate, float amount){
this.fromdate= fromdate;
this.todate= todate;
this.amount = amount;
}}
May i ask if i use constructor to create a new example class, that is
example ex = new example (fromdate, todate, amount)
Can i access the private fields declared for the ex variable just by
ex.fromdate, ex.todate, ex.amount ??
When and why do we need to declare a getter and setter method to get the variables inside the object while we can directly use the dot notation to get the fields inside a class?
Thanks a lot
---------------------------EDIT--------------------------------------
One more question guys,
May i ask if i can directly use dot notation to get the private fields if i nest the above class inside another class??
for example:
public class ExampleOutterClass{
private class Example(...){...}
---Some Operation to get fromdate, todate, amount)---
Example ex = new Example (fromdate, todate, amount);
public Timestamp fromdate = ex.fromdate;
public Timestamp todate = ex.todate;
public float amount = ex.amount;
}
I am trying the above practise in my Jbuilder (very old developer tools ORZ), and found it's weird that i can directly use dot notation to refer to the private fields, so i raise up this question.
BTW Really thanks for the help