Getting familiar with Java: How to store different values inside class variables for different instances

In the earlier two series of getting familiar with Java we learn miniature concepts of classes, variables and other elements that should be there with Java programming. In this tutorial we will be reading in detail about classes and objects in Java. Class is the systematic structure with which objects can be initiated. Properties of class are known as attributes and functions which are related with properties of classes or attributes are known as methods.

Class can be defined as the fully-featured concept that should be included within an application. Suppose we want to build an application related with hospital management. Here concepts are patient registration, patient databases and so on. These concepts are separate classes which we will like to build within an application.

This means that we have a separate patient registration class, patient database class and so on. Then comes the concept how to distinguish one class from the other in the object. Classes are separated from each other through the concept of properties of the classes. Here in the class of patient registration the property of patient registration will be patient id. Similarly, in the case of patient database class the property of patient databases class will be name of patient and so on.

In this way we know how classes are separated with each other through properties of classes. Property of classes are used to distinguish with each other from class and these are known as attributes in Java languages. How these attributes are defined within the classes? These attributes are defined with the help of variables. This means with the help of variables the property or attribute of classes are defined.

For each class the attributes and its instances can be different. For example, when we consider patient registration class, it has suppose 10 patients with the different ids. We use instances in these attributes to differentiate with the attributes or properties of classes. Here we take classes as patients and patient ids as properties or attributes. With the help of objects we take different instances for different variables within same class.

The functions which act within these attributes or properties are called as methods. In java we will call function as methods. This means in order to run the object or create an application there will be different classes which are differentiated with the help of attributes or properties and then these attributes are differentiated with the help of instances of methods. When we think the hospital management app as object then there will be patient registration and properties of patient databases can be signs of different classes and then the attribute of one calls such as patient registration can be patient ids and with the same attributes we can have different methods or functions.

How to declare a class?

In order to declare class we have to first use class keyword.Class key word is ‘Class’ and then mention the name of class. Here Patient is class name. In the class name we can different properties or attributes. Like patient ids, which can be int variable and Faculty name can be string variable. This is the process to declare the class.

 

Class -keyword

Patient -Class name

Int Fac_it-Property or attribute of class patient

String Fac_name-Property or attribute of class patient

Class Patient

          int Fac_id;

          String Fac_name

 

How to create objects of class?

For a particular instance a variable can have the different instances for different values of a class. An object is the instance of a class. For different variables what should be the values of the class at that particular instance. In order to add object to the class we have to add a new operator. First we should write the class name as we have taken it as Patient. So, first we should write the class name and then object name, which can be pat1 and then we create new operator with the same name of class.

New operator has always been followed by the name of the class and then use the constructor (), if you have doubt about it then please go through my previous article related with java in this website and in the following days, I should be taught you in detail about constructor. In the end of this statement we have semicolon; which means end of the statement.

So first we use class keyword and then name of object and then new operator as new and then it is followed by class name and then constructor and semicolon.

Patient Pat1=new Patient();  Here the object created with the name Pat1. We can create another object as follows.

Patient Pat2=new Patient();

So here we created two objects of Patient registration class in the name of Pat1 and Pat2. So we have created two instances of class. One instance is represented by Pat1 and the other one is represented by Pat2. Now we can distribute different values to variables with association of class.

Here ours firs instance or object of class is Pat1 and we will associate variable id. Let us see the following example.

Here, we can create:

Pat1.id=2

Pat1.pat.name=’Abc’

Here we have associated the Pat1 object of instance of class with variable id and put the values to it. Every variables contains values and please refer to previous article on it. Then in second we added another value to the class instance or object Pat1 with the variable name by associating class with variable and provide the name of the Pat1. Similarly we can do this to another instance Pat2.

Pat2.id=3

Pat2.pat.name=’Xyz’

In this way with the help of variables and by attaching variables with the class objects we can provide different values to same instances.

How to add method to a class?

General Java programming form of adding methods or functions to a class is:

  • MethodName
  • Parameter list
  • Method body

There are different returntype for adding methods to class. Such as void.int. You can use any returtype in front of method name. Most of times we call methods with the help of objects such as Pat1 as we created the object in the above. Suppose I have one method namely m1. Here is the way to call the methods through object.

Pat1.m1();

This is the way to call methods through object. Here object is Pat1 and the method is m1. So we have called the method from object.

How to store different values inside class variables for different instances or objects of a class in a completely programming guide

First we will create Patient class and in that class we will create two variables in the name of faculty name and faculty id. Then we will create the method which should display the values of variables. Then methods will be called with the help of objects. Here is the complete coding part:

 

Class faculty

{

           int pat_id;

           String pat_name;

           void display()

                 {

                      System.out.println(“Patient Id: “+ pat_id);

                      System.out.println(“Patient Name: “+ pat_name);

}

         Public static void main(String a[])

           {

                 patient p1=new patient();

                 p1.pat_id=1;

                 p1.pat_name=”Mohan”;

                 patient p2=new patient();

                 p2.pat_id=2;

                 p2.pat_name=”Manohar”

                 p1.display();

                 p2.display()     

}



}

Here to create the class I use class key word. The patient is the class name. Parameters of class attributes or properties are variables within the class. Here I created two variables such as Pat_id of variable type print and Pat_name of variable type string. Then for method or for function I create display method. Here, in order to display methods I used properties name of class Patient. The two properties of patient class is that of tow variables in order to show it one is pac_id which is nothing but patient id and the other is pat_name which is nothing but name of patient.

How to create object of class patient:

To create the object of the class as we have discussed earlier first use the class name which is patient and then object where I use p1 as object name and then use new and then call the constructor with the same name of class. At the end of statement there is semicolon.

patient p1=new patient();

Now, values to be assigned to variables. So both the objects should be clearly defined and so that these will become instances of the class patient.

 

                 patient p1=new patient();

                 p1.pat_id=1;

                 p1.pat_name=”Mohan”;

                 patient p2=new patient();

                 p2.pat_id=2;

                 p2.pat_name=”Manohar”

 

Here, I created two objects such as p1 and p2 and for p1 patient id is 1 and the patient name is Mohan and for p2 patient id is 2 and patient name is Maohar.

Now we will call both objects with the help of functions or methods which we have written above which is

void display()

                 {

                      System.out.println(“Patient Id: “+ pat_id);

                      System.out.println(“Patient Name: “+ pat_name);

}

 

Now we will call both objects p1 and p2 with the help of functions which we have written above such as

 

void display()

The code of calling objects through functions is that

                 p1.display();

                 p2.display()

if everything goes right then with the execution of code the output will be after compilation of code will be:

Patient id=1

Patient name: Mohan

Patient id=2

Patient name=Manohar

Here instances mean different objects of a class. In this programming tutorial we have stored different values inside different variables or attributes of the class and then assign values to different instance of class such as objects.

How to store different values inside class variables for different instances or objects of a class.You can create different types of such programming codes through this references and put the queries here so that I will try to help you in solving of compilation of code.

About Mohan Manohar Mekap

Mohan Manohar is a blogger from India who founded Ittech back in 2007. He is passionate about all things tech and knows the Internet and computers like the back of his hand.

Leave a Reply

Your email address will not be published. Required fields are marked *

IndiChange - Harnessing the collective power of blogging to fight evil.