What is meant by a Class Member ? Is it the class ...

Discussion in 'Big Data and Analytics' started by Indranil Roy, Aug 12, 2016.

  1. Indranil Roy

    Indranil Roy Member
    Alumni

    Joined:
    Jul 17, 2016
    Messages:
    5
    Likes Received:
    0
    What is meant by a Class Member ? Is it the class variable that is declared. In that case what is the difference between Public & Protected ? Protected can be accessed by other members in the package ( which are basically other classes ) and Public definition is also same ?
     
    #1
  2. DeshDeep Singh

    DeshDeep Singh Well-Known Member
    Simplilearn Support Alumni

    Joined:
    Sep 22, 2015
    Messages:
    935
    Likes Received:
    67

    Hi Indranil,

    Understanding Class Members
    In this section, we discuss the use of the static keyword to create fields and methods that belong to the class, rather than to an instance of the class.

    Class Variables
    When a number of objects are created from the same class blueprint, they each have their own distinct copies of instance variables. In the case of the Bicycle class, the instance variables are cadence, gear, and speed. Each Bicycle object has its own values for these variables, stored in different memory locations.

    Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.

    For example, suppose you want to create a number of Bicycle objects and assign each a serial number, beginning with 1 for the first object. This ID number is unique to each object and is therefore an instance variable. At the same time, you need a field to keep track of how many Bicycle objects have been created so that you know what ID to assign to the next one. Such a field is not related to any individual object, but to the class as a whole. For this you need a class variable, numberOfBicycles, as follows:

    public class Bicycle {

    private int cadence;
    private int gear;
    private int speed;

    // add an instance variable for the object ID
    private int id;

    // add a class variable for the
    // number of Bicycle objects instantiated
    private static int numberOfBicycles = 0;
    ...
    }

    Class variables are referenced by the class name itself, as in

    Bicycle.numberOfBicycles

    This makes it clear that they are class variables.

    Note: You can also refer to static fields with an object reference like
    myBike.numberOfBicycles

    but this is discouraged because it does not make it clear that they are class variables.
    You can use the Bicycle constructor to set the id instance variable and increment the numberOfBicycles class variable:

    public class Bicycle {

    private int cadence;
    private int gear;
    private int speed;
    private int id;
    private static int numberOfBicycles = 0;

    public Bicycle(int startCadence, int startSpeed, int startGear){
    gear = startGear;
    cadence = startCadence;
    speed = startSpeed;

    // increment number of Bicycles
    // and assign ID number
    id = ++numberOfBicycles;

    }

    // new method to return the ID instance variable
    public int getID() {
    return id;
    }
    ...
    }
     
    #2
  3. Rahul Rai_4

    Rahul Rai_4 New Member

    Joined:
    Sep 26, 2016
    Messages:
    1
    Likes Received:
    0
    Class members means variables and methods in the class. Public members can be accessed from anywhere. But protected members can be accessed from within package and also it can be accessed from outside package but through inheritance only.
     
    #3

Share This Page