1

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

ProgrammingBaKa
  • 363
  • 2
  • 19
  • Your constructor seems fine. The idea of being `private` is so that you can **not** access then from outside the class itself unless you implement public getter methods. It is usually desirable to hide the internal workings of your class. – Scary Wombat Mar 10 '17 at 01:38
  • Java coding conventions say that your class names should start with a capital letter and variables with small letters. So you class name should be `Example` and it is easier to read and understand. :) – Sufian Mar 10 '17 at 01:52
  • 1
    Nested classes are not exactly members of outer classes, but they are very similar. Just like members can access any other member (any method can invoke other, even private method declared in same class) nested classes can access all other members, and be accessed by them. – Pshemo Mar 10 '17 at 02:44

4 Answers4

0

Because private fields can not access from outside the class. You are not able to accesss just by :

ex.fromdate, ex.todate, ex.amount

Technically, you can make them public and use dot notation to get what you want conveniently . Why are we use "getter" and "setter" ? you may think they are two common methods that most program accept and they can retrict you access the class member regularly. And the most important , is useful in the some design patten. You can learn more about Javabean about setter and getter.https://en.wikipedia.org/wiki/JavaBeans

Jswq
  • 758
  • 1
  • 7
  • 23
0

You are not allowed to access any private member (field, method) of a class.

If you need to control access to the private members of your class then you have to declare them private and provide methods to manipulate instances of the class.

One way of "controlling" access to the private members is to define setters and getters. But in this case, you still allow everyone to change an object's inner state. For example:

public class Point {
  private double x;
  private double y;
  public Point(double x, double y) {
    this.x = x;
    this.y = y;
  }
  public void setX(double x) {
    this.x = x;
  }
  public double getX() {
    return x;
  }
  // ... setter and getter for y
}

Another way is to define good public API which can be used by others. In this case, only your class can change its inner state. For example:

public class Point {
  private double x;
  private double y;
  public Point(double x, double y) {
    this.x = x;
    this.y = y;
  }
  public void move(double deltaX, double deltaY) {
    // some validations can be applied here
    x += deltaX;
    y += deltaY
  }
}
vhula
  • 487
  • 2
  • 9
-1

No, you cannot access the private members of a class directly from an object outside your class. Private members can only be accessed by functions within the same class. Getters and Setters are created as public methods which get or set these values. Other classes should use these getters and setters to modify the private members.

One question you may have is :Why do we need getters and setters when you can make the variable public itself and then access it from another class? Please follow this question for that : Why use getters and setters?

Community
  • 1
  • 1
Ravi Sanker
  • 84
  • 1
  • 5
-1

Private fields are not accessible outside of the class they are defined in. In order to access those fields you may create getter or setter methods instead. Getter and setter methods are often used when you want to either have the private member be read only or if you want the member's value to be set but not read.

Setter and getter methods are often also used when you want to modify a value before returning it or setting it. Ravi linked a great answer on why to use getter and setter methods instead of simply making a member variable public.

Walker Christie
  • 531
  • 1
  • 3
  • 18