SEEM3460 Tutorial Java Keyword – static. static Variables class XY { static int x; // class variable int y; // instance variable } XY xy1 = new XY();

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

For(int i = 1; i
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
Local Variables and Scope Benjamin Fein. Variable Scope A variable’s scope consists of all code blocks in which it is visible. A variable is considered.
Author: Takdir, S.ST. © Sekolah Tinggi Ilmu Statistik.
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Nested Classes Yoshi Modified from:
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Access to Names Namespaces, Scopes, Access privileges.
Final and Static Keywords. Creating constants  There will be occasions where data items in a program have values that do not change.  Maximum score.
Enhancing classes Visibility modifiers and encapsulation revisited
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
Unit 081 Introduction to Nested Classes Nested classes are classes defined within other classes The class that includes the nested class is called the.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
The different kinds of variables in a Java program.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
The different types of variables in a Java program.
COMP More About Classes Yi Hong May 22, 2015.
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
More on Classes Pepper With help from rs.html.
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.
Object Oriented Programming: Java Edition By: Samuel Robinson.
SEEM3460 Tutorial Arrays in Java. Arrays in Java Initialization array1 = new SomeClass[ARRAY_SIZE]; array1 = {element1, element2, …}; Object as elements.
Chapter 4 Procedural Methods. Learning Java through Alice © Daly and Wrigley Objectives Identify classes, objects, and methods. Identify the difference.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
5x 4 – 405 = 5(x 4 – 81) = 5(x 2 + 9)(x 2 – 9) = 5(x 2 + 9)(x + 3)(x – 3) by D. Fisher.
MSIS 655 Advanced Business Applications Programming Week 1 Introduction to Java
Factoring Perfect Square Trinomials
Multiplying Polynomials -Distributive property -FOIL -Box Method.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
Lec 14 Writing an Instantiable Class III. Agenda Review of Instantiable Classes Scope of variables Using this to override scope issues Lab: Creating a.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
AP Computer Science A – Healdsburg High School 1 static Keyword in Java - Static variables - Static methods.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Swing Threading Nested Classes COMP 401 Fall 2014 Lecture 23 11/25/2014.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
3 ( x + 2 ) = 3 x ( 3 x - 5 ) = 6x - 10 x x x - 5 M May.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
CPSC 233 Tutorial 12 March 4/5 th, TopHat Quiz int[] a = {0}; int[] b = {1}; a = b; What is the value of a[0] i) 0 ii) 1.
Python analysis Group Four Siy, Sherwin Cagaoan, Allen Sanidad, Rizzi
I can’t hear you through the static
The Lifetime of a Variable
CompSci 230 S Programming Techniques
Using local variable without initialization is an error.
Chapter 4 Procedural Methods.
Outline Altering flow of control Boolean expressions
Building Java Programs
Chapter 7 Procedural Methods.
Namespaces, Scopes, Access privileges
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Method of Classes Chapter 7, page 155 Lecture /4/6.
Building Java Programs
Unit-2 Objects and Classes
Object Oriented Programming (OOP) Lecture No. 12
Presentation transcript:

SEEM3460 Tutorial Java Keyword – static

static Variables class XY { static int x; // class variable int y; // instance variable } XY xy1 = new XY(); XY xy2 = new XY(); XY.x, xy1.x, xy2.x all refer to same int using XY.y gives an error xy1.y and xy2.y refer to different int

static Methods class XY { static int x() {…}; // class method int y() {…}; // instance-bounded method } XY xy1 = new XY(); XY xy2 = new XY(); XY.x(), xy1.x(), xy2.x() are the same calling XY.y() gives an error xy1.y() and xy2.y() calls the same methods but with different scopes (encapsulation) keyword this can be used in y, but not x

static Classes (Advanced, FYI) class XY { static class X {…}; class Y {…}; } X is a class nested in XY X can be used like a normal class of the name XY.X Y is a Inner class of Outer class XY Y cannot be used without an instance of XY each instance of Y must be attached to an instance of XY, the containing instance of XY can be referred by XY.this

Exercise Based on CD create: static double totalCost; static CD mostExpensiveCD; double Cost; // this already exists static int roundedTotalCost(); int roundedCost(); Update the constructor for making use of the new variables