ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Advertisements

1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Variable types We have already encountered the idea of a variable type. It is also possible to think about variables in terms of their kind, namely: 1)
Chapter 10 Classes Continued
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Multiple Choice Solutions True/False a c b e d   T F.
Intro to OOP with Java, C. Thomas Wu
Static Keyword. What is static The static keyword is used when a member variable of a class has to be shared between all the instances of the class. All.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
The Java Programming Language
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
Programming using C# for Teachers Introduction to Objects Reference Types Functions of Classes Attributes and Types to a class LECTURE 2.
A First Simple Program /* This is a simple Java program. Call this file "Example.java".*/ class Example { // Your program begins with a call to main().
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
CSC 142 Computer Science II Zhen Jiang West Chester University
C# G 1 CSC 298 Object Oriented Programming Part 2.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
1 CS 177 Week 11 Recitation Slides Class Design/Custom Classes.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
CSI 3125, Preliminaries, page 1 Compiling the Program.
Topics Inheritance introduction
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming Final Keyword In Java. final keyword The final keyword in java is used to restrict the user. The final keyword can be used in many context.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Classes - Intermediate
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 9 Lecturer: Dr. Emad Nabil 1-1.
Defining Your Own Classes II
Modern Programming Tools And Techniques-I
Abstract classes and interfaces
Scope and Code Generation
Using local variable without initialization is an error.
CSC240 Computer Science III
CSC 113 Tutorial QUIZ I.
More inheritance, Abstract Classes and Interfaces
Abstract classes and interfaces
Interface.
METHOD OVERRIDING in JAVA
Sampath Kumar S Assistant Professor, SECE
class PrintOnetoTen { public static void main(String args[]) {
Building Java Programs
Abstract classes and interfaces
Sampath Kumar S Assistant Professor, SECE
Consider the following code:
Chapter 11 Inheritance and Encapsulation and Polymorphism
Corresponds with Chapter 5
Presentation transcript:

ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya

Introduction How and where do we use static keyword To variables To methods Advantages of using Static keyword Summary

 Static is a keyword that states that all instances of a given class are to share the same variable or method.  This is used for a constant variable or a method that is the same for every instance of a class  This presentation will provide information about uses of Static keyword in JAVA

 Without the “static” keyword, it's called “instance variable”, and each instance of the class has its own copy of the variable.  When a variable is declared with the static keyword, its called a “class variable”. All instances share the same copy of the variable

 A common use of static variables is to define "constants“.  A class variable can be accessed directly with the class, without the need to create a instance.  Variables can be declared with the “static” keyword like this. Example: static int y=0;

 Methods declared with “static” keyword are called “class methods”. Otherwise they are “instance methods”.  Methods declared with static cannot access variables declared without static. public class Main { static int xl = 0; int x = 20; public static void m() { x = 150; //non static x cannot be access } public static void main(String[] args) { m(); }

public class Main { public int m (){ int x = 20; return x; } public static void main(String[] args){ Main ws = new Main(); ws.m(); } The following gives a compilation error, unless m method is also static. Or unless it is accessing through object public class Main { public int m (){ int x = 20; return x; } public static void main(String[] args) { m (); } A Note: There's no such thing as static classses. “static” in front of class creates compilation error.

***Remember that static methods can’t be OVERRIDDEN. But that doesn’t mean that they cant be redefined in a sub class. class Main { static void travel() { System.out.print("A"); } class main extends subMain { static void travel() { System.out.print("B"); //this is redefinition NOT overridden }

Static variables can be use to hold constants (values that are not changing). Static method’s behavior has NO dependency on the state of an object. That mean methods which are declared as static will always runs the same way.

Documentation. Anyone seeing that a method is static will know how to call it. Any programmer looking at the code will know that a static method can't interact with instance variables, which makes reading and debugging easier. Efficiency. A compiler will usually produce slightly more efficient code because no implicit object parameter has to be passed to the method.

Summary Q & A

Thank You