Simple Classes in Java CSCI 392 Classes Part

  • Slides: 11
Download presentation
Simple Classes in Java CSCI 392 Classes – Part 1

Simple Classes in Java CSCI 392 Classes – Part 1

Today's Objectives 1. Scope of Variables and Methods public, private, and more 2. Constructors

Today's Objectives 1. Scope of Variables and Methods public, private, and more 2. Constructors 3. Instance Variables and Class (static) Variables

Class Code usually goes in its own File sample. java widget. java public class

Class Code usually goes in its own File sample. java widget. java public class widget { blah } public class sample { main () { widget bob = new widget; bob. somemethod(); } } n The file "widget. java" contains all the code for the widget class. No main(). n First, compile widget. java into widget. class n Sample. java does not need to "include" widget. java or widget. class

Limiting Scope of Fields Given a class named "list". . . n public int

Limiting Scope of Fields Given a class named "list". . . n public int size; n everyone can see size n private int size; n only list instances can see their size, default is private n protected int size; n only list instances and classes derived from list can see size; i. e. somewhat private n public final double pi = 3. 14; n pi is a constant visible to everyone

Constructors n Syntax is just like C++ public class list { private int size

Constructors n Syntax is just like C++ public class list { private int size = 0; public list () { size = 0; } // field // constructor n Constructors are optional in Java (and C#) n this constructor sets size=0, which is the default anyway

Initializers n Since constructors are optional… public class list { private int maxsize =

Initializers n Since constructors are optional… public class list { private int maxsize = 10; public list () { maxsize = 10; } // set default // redundant public list (int newmax) { maxsize = newmax; // override default }

Creating an Object n Don't forget to call new() class Main. Program { public

Creating an Object n Don't forget to call new() class Main. Program { public void Main() { list mylist 1 = new list(); list mylist 2 = new list(50); . . .

"this" = this instance public class list { private int size; . . .

"this" = this instance public class list { private int size; . . . public set_size (int size) { this. size = size; }

Instance Variables vs Class Variables Sometimes, you need one variable that belongs to the

Instance Variables vs Class Variables Sometimes, you need one variable that belongs to the entire class, not separate variables for each instance. public class Warning. Box { private String message; private static int boxcount; Each Warning. Box instance has its own message, but there is only one boxcount shared by all Warning. Boxes.

Static Methods n Operate on the class, not an individual instance. n Not allowed

Static Methods n Operate on the class, not an individual instance. n Not allowed to access non-static variables. static private boolean Too. Many. Boxes() { if (boxcount > 10) return true else return false; }

Quiz 1. Are variables and methods public or private by default? 2. Why do

Quiz 1. Are variables and methods public or private by default? 2. Why do I need a constructor if I can initialize fields when I declare them? 3. How is a "class" variable declared? 4. Given "private static int Box. Count" is this legal: "this. Box. Count = 0; " ?