JAVA @ ICBT Basura Ramanayaka Eshani werapitiya Hasitha Dananjaya.

12
Using Static keyword in JAVA JAVA @ ICBT Basura Ramanayaka Eshani werapitiya Hasitha Dananjaya

Transcript of JAVA @ ICBT Basura Ramanayaka Eshani werapitiya Hasitha Dananjaya.

Page 1: JAVA @ ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.

Using Static keyword in

JAVA

JAVA @ ICBT

Basura Ramanayaka

Eshani werapitiya

Hasitha Dananjaya

Page 2: JAVA @ ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.

Overview

• Introduction

• How and where do we use static

keyword

• To variables

• To methods

• Advantages of using Static keyword

• Summary

Page 3: JAVA @ ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.

Introduction 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

Page 4: JAVA @ ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.

How and where we use static keyword to

variables

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

Page 5: JAVA @ ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.

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;

Page 6: JAVA @ ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.

How and where we use static keyword

to MethodsMethods 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(); }}

Page 7: JAVA @ ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.

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 B

Note: There's no such thing as static classses. “static” in front of class creates compilation error.

Page 8: JAVA @ ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.

***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 }}

Page 9: JAVA @ ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.

Advantages of using Static keyword

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.

Page 10: JAVA @ ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.

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.

Page 11: JAVA @ ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.

Summary

Q & A

Page 12: JAVA @ ICBT  Basura Ramanayaka  Eshani werapitiya  Hasitha Dananjaya.

Thank You