0% found this document useful (0 votes)
31 views17 pages

Lecture 4 - Programming Syntax

The document discusses object oriented programming concepts like classes, objects, encapsulation, and variables in Java. It provides examples of how to declare classes with attributes and methods, how to create objects from classes, and how to encapsulate attributes. It also explains the different types of variables in Java - static/class variables, instance variables, and local variables - and provides examples of how each variable type is declared and used. The document was presented by J. Jennifer Ranjani from BITS Pilani for their CS F213 class.

Uploaded by

AYUSH AGRAWAL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views17 pages

Lecture 4 - Programming Syntax

The document discusses object oriented programming concepts like classes, objects, encapsulation, and variables in Java. It provides examples of how to declare classes with attributes and methods, how to create objects from classes, and how to encapsulate attributes. It also explains the different types of variables in Java - static/class variables, instance variables, and local variables - and provides examples of how each variable type is declared and used. The document was presented by J. Jennifer Ranjani from BITS Pilani for their CS F213 class.

Uploaded by

AYUSH AGRAWAL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

Object Oriented Programming

CS F213
J. Jennifer Ranjani
email: [email protected]
BITS Pilani
Pilani Campus
Class/Object Example
class Student{  
  int id;  
  String name;  
}  
class TestStudent{  
  public static void main(String args[]){  
//Creating object  
   Student s1=new Student();  
//Initializing object  
   s1.id=253;  
   s1.name="Sathish";  
//Printing data  
   System.out.println(s1.id+" "+s1.name);  
    }
}  

BITS Pilani, Pilani Campus


Encapsulation Example
class Student{  
 private int rollno;  
 private String name;  
 void insertRecord(int r, String n){  
  rollno=r;  
  name=n;    }  
 void displayInformation(){
System.out.println(rollno+" "+name);}  
}  
class TestStudent{  
  public static void main(String args[]){  
   Student s1=new Student();  
    s1.insertRecord(111,"Karan");  
   s1.displayInformation();  
    }  
}  

BITS Pilani, Pilani Campus


Programming Syntax
BITS Pilani
Pilani Campus
Naming Convention (Rule)

• Used to name your identifiers such as class, package,


variable, constant, method etc.
Name Convention
class name should start with uppercase letter and be a noun e.g.
String, Color, Button, System, Thread etc.
interface name should start with uppercase letter and be an adjective e.g.
Runnable, Remote, ActionListener etc.

method name should start with lowercase letter and be a verb e.g.
actionPerformed(), main(), print(), println() etc.

variable name should start with lowercase letter e.g. firstName,


orderNumber etc.
package name should be in lowercase letter e.g. java, lang, sql, util etc.

constants name should be in uppercase letter. e.g. RED, YELLOW,


MAX_PRIORITY etc.

BITS Pilani, Pilani Campus


Variable

• Name given to a memory


location. It is the basic unit of
storage in a program.
• The value can be changed during program
execution.
• All the operations done on the variable effects
that memory location.
• Variables must be declared before they can be
used. (but not necessarily in the beginning)

• Types of variables
• Static or class variables
• Instance Variables
• Local variables

BITS Pilani, Pilani Campus


Static Variables

• A class variable is any field declared with the static


modifier

• Tells the compiler that there is exactly one copy of this


variable in existence, regardless of how many times the
class has been instantiated.

BITS Pilani, Pilani Campus


Static Variables-Example
public class StaticVarEx {
static String myClsVar="class or static variable";

public static void main(String args[]){


StaticVarEx obj1 = new StaticVarEx();
StaticVarEx obj2 = new StaticVarEx();

System.out.println(obj1.myClsVar);
System.out.println(obj2.myClsVar); System.out.println(myClsVar);

//changing the value of static variable using obj2


obj2.myClsVar = "Changed Text";
Output:
System.out.println(obj1.myClsVar); class or static variable
System.out.println(obj2.myClsVar); } } class or static variable
Changed Text
Changed Text
BITS Pilani, Pilani Campus
Static variable across
classes-Example
class Zero{
static String classvar = "Static Variable of another class";
}
class First{
public static void main(String args[]){
System.out.println(Zero.classvar);
}
}

BITS Pilani, Pilani Campus


Where static variables can
be used?
• Counting the number of objects

• Specify the college or dept name for all the student


objects

• Specify the branch name and code for all account


holders of a particular branch.

BITS Pilani, Pilani Campus


Instance (Non-static)
Variables
• Objects store their individual states in "non-static fields"

• Fields declared without the static keyword.

• Non-static fields are also known as instance variables


because their values are unique to each instance of a
class.

BITS Pilani, Pilani Campus


Instance Variables-Example
public class InstanceVarExample {
String myInstanceVar=“instance variable";

public static void main(String args[]){


InstanceVarExample obj1 = new InstanceVarExample();
InstanceVarExample obj2 = new InstanceVarExample();

//Two objects will display "class or static variable"


System.out.println(obj1.myInstanceVar);
System.out.println(obj2.myInstanceVar);

//changing the value of static variable using obj2


obj2.myInstanceVar = "Changed Text"; Output:
instance variable
//All will display "Changed Text" instance variable
instance variable
System.out.println(obj1.myInstanceVar); Changed Text
System.out.println(obj2.myInstanceVar); } }
BITS Pilani, Pilani Campus
Local Variable

• Defined within a block or method or constructor

• These variable are created when the block in entered or


the function is called and destroyed after exiting from the
block or when the call returns from the function.

• The scope of these variables exists only within the block


in which the variable is declared. i.e. we can access
these variable only within that block.

BITS Pilani, Pilani Campus


Local Variable-Example
public class VariableExample {

// instance variable
public String myVar="instance variable";

public void myMethod(){


// local variable
String myVar = "Inside Method";
System.out.println(myVar); }

public static void main(String args[]){

// Creating object
VariableExample obj = new VariableExample();
System.out.println("Calling Method"); Output:
Calling Method
obj.myMethod(); Inside Method
Instance variable
System.out.println(obj.myVar);
}
} BITS Pilani, Pilani Campus
Queries asked during the
lecture
• Can we access static variables before object creation? If
yes, when will the memory for such variables be
allocated.

Answer: Memory allocation for static variables happen


at compilation time and memory allocation for
instance variable happen at run time. Hence, static
variables can be accessed before objects are created.

BITS Pilani, Pilani Campus


Queries asked during the
lecture
• Can we modify static variables?
• Of course. We can. There are no restrictions in
modifying the static variables.

• NOTE: Not all static methods are called before the


instances are created. Only the main method with public
static void signature is called before any instances are
created. We shall see more on static methods in the
future sessions.

BITS Pilani, Pilani Campus


Queries asked during the
lecture
• Can static variables be encapsulated?

• Yes. You can also be encapsulated like the instance


variables.

BITS Pilani, Pilani Campus

You might also like