Static Data Member - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

Static Data Member

Description:

If a variable is declared as static, only one copy of the variable exists ... Student s2 = new Student('Shay','Eilat'); s2.print(); System.out.println(s1.HowMany ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 8
Provided by: uazu
Category:
Tags: data | member | shay | static

less

Transcript and Presenter's Notes

Title: Static Data Member


1
Static Data Member
  • Static variables are sometimes called class
    variables
  • Normally, each object has its own data space
  • If a variable is declared as static, only one
    copy of the variable exists
  • private static float price
  • Memory space for a static variable is created as
    soon as the class in which it is declared is
    loaded

2
Static Data Member
  • All objects created from the class share access
    to the static variable
  • Changing the value of a static variable in one
    object changes it for all others
  • Static methods and variables often work together

3
Student
  • public class Student
  • private static int staticNumber 0
  • private int studentNumber
  • private String name,address
  • Student(String name,String address)
  • this.name name
  • this.address address
  • studentNumber staticNumber
  • staticNumber
  • public void print()
  • System.out.println("Name " name)
  • System.out.println("Address " address)
  • System.out.println("Number " studentNumber
    )

4
TestStudent
  • public class TestStudent
  • public static void main(String args)
  • Student s1 new Student("Moshe","Beer-Sheva")
  • s1.print()
  • Student s2 new Student("Shay","Eilat")
  • s2.print()
  • System.out.println(s1.HowMany())
  • System.out.println(s2.HowMany())
  • System.out.println(Student.HowMany())

5
StringTokenizer
  • The StringTokenizer class makes it easy to break
    up a string into pieces called tokens.
  • By default the delimiters for the tokens are the
    white space characters (space, tab, new line)
  • The StringTokenizer class is defined in the
    java.util package

6
StringTokenizer
  • StringTokenizer st new StringTokenizer("this is
    a test")
  • hasMoreTokens() Tests if there are more tokens
    available from this tokenizer's string.
  • nextToken() Returns the next token from this
    string tokenizer.

7
StringTokenizer
  • import java.util.
  • public class Poem
  • public static void main(String args)
  • String poem
  • "Que sera sera, whatever will be will be"
  • StringTokenizer tokenizer
  • new StringTokenizer(poem)
  • String token
  • while (tokenizer.hasMoreTokens())
  • token tokenizer.nextToken()
  • System.out.println(token)
Write a Comment
User Comments (0)
About PowerShow.com