[PDF] [PDF] Static Methods and Data - MIT OpenCourseWare

Static is a historic keyword from C and C++ Static variables in C, C++ are different than in Java Overloading is general; it s not related to static vs instance



Previous PDF Next PDF





[PDF] CSE 2221 - Static vs Instance Methods - OSU CSE

7 jan 2019 · This is why an instance method seems to have one less formal parameter than a static method with exactly the same functional behavior Recall that polymorphism is the mechanism that selects the method body to be executed based on the dynamic/object type of the receiver



[PDF] 1 Static vs Instance Methods

2 mar 2003 · There is one immediately obvious difference between Scheme and Java ( besides the different syntax) Java is a stronly-typed language This 



[PDF] Chapter 5 Static Methods

A static method still belongs to a class, and its definition is given inside the class There is only one copy of a static variable per class, unlike instance i bl h h bj th it It is found in the java lang package, so it does not require an import 



[PDF] Static Methods and Data - MIT OpenCourseWare

Static is a historic keyword from C and C++ Static variables in C, C++ are different than in Java Overloading is general; it s not related to static vs instance



[PDF] Static vs Dynamic Type - EECS: www-insteecsberkeleyedu

to try to provide a generalized method of following Java as it dives into the other words, the compiler looks inside the definition of the class StaticClass and have an instance that has a static type and a relatively-more-specific dynamic type,



[PDF] ICS 111 Java Classes

myString may hold a different value from b myString ○ the variable myString is an instance variable: each instance of the class (each object of an instance method (a method declared without static) can use the instance variables of a class



[PDF] Static class in Java

In java, we have static instance variables as well as static methods and also static What are the differences between static and non-static nested classes?



[PDF] Static Methods and Data - MIT OpenCourseWare

Static is a historic keyword from C and C++ Static variables in C, C++ are different than in Java Overloading is general; it s not related to static vs instance



[PDF] Chapter 2 Dual Roles of a Class - UNC Computer Science

depth at various concepts in Java in particular and object-oriented languages, in general, two different classes to define methods with the same name In the case of static methods, it indicates the class in which the method should be and instance variables is that the former are units of the external state of an object, 



[PDF] Difference between Static and non-static method in Java

*In case of non-static method memory is allocated multiple time whenever method Method of a class can be declared in two different ways instance method

[PDF] difference between tertiary and quaternary structure of protein

[PDF] difference between transact sql and sql server

[PDF] difference between variable and attribute

[PDF] difference between variables and attributes in quality control

[PDF] difference between variables and attributes in research

[PDF] difference between web tier and app tier

[PDF] différence entre caractéristiques et propriétés

[PDF] différence entre mitose et méiose

[PDF] différence entre mitose et méiose 3eme

[PDF] différence entre mitose et méiose pdf

[PDF] différence entre mitose et méiose tableau

[PDF] difference entre pharmacodynamie et pharmacocinétique

[PDF] difference fonction convexe et concave

[PDF] différence imparfait et passé simple espagnol

[PDF] difference in acidity of alcohols phenols and carboxylic acids

1

1.00 Lecture 10

Static Methods and Data

Reading for next time: Big Java: sections 7.1-7.4, 7.6, 7.8

Strings

p public class StringExample { public static void main(String[] args) { String s= new String("Test"); // Strings are objects String first= "George "; // Shortcut constructor

String middle= "H.W. ";

String last= "Bush";

String full= first + middle + last;

System.out.println("Full: " + full);

// Testing for equality in strings (objects in general)

String full2= "George H.W. Bush";

if (full.equals(full2)) // Right way

System.out.println("Strings equal");

if (full == full2) // Wrong way

System.out.println("A miracle!");

if (first == "George ") // Wrong way,but sometimes works System.out.println("Not a miracle!"); // Unreliable // Modifying strings must be done indirectly-strings are constants middle= middle.substring(2, 4) + " "; // Get 2nd, 3rd chars full= first + middle + last; System.out.println("Modified full: " + full); } } // See String documentation on javadoc 2

Static Class Methods, Data

Static data fields:

Only one instance of data item for entire class

Not one per object

Static is a historic keyword from C and C++

Class data fields is a better term

These are the alternative to instance data fields (which are a field in each object)

Static methods:

Do not operate on objects and do not use any specific object

Have access only to static data fields of class

Cannot access instance fields in objects

You can pass arguments to static methods, as with all methods

Class methods is a better term

These are the alternative to instance methods (that operate on an object)

When to Use Static Data

Variables of which there is only one for a class

For example, the next ID number available for all MIT students (assuming they are issued sequentially). In a

SStudent class:

public class Student { private String name; // 1 value per instance private int ID; // 1 value per instance private static int nextID=1; // 1 value per class public static int getID() { return nextID++;}

Constants used by a class (final keyword)

Have one per class; dont need one in each object

public static final int MAX_TERMS_AS_STUDENT= 16; public static final double ABSOLUTE_ZERO= 273.0; If ABSOLUTE_ZERO is in class Temperature, it is invoked by double tKelvin= Temperature.ABSOLUTE_ZERO + tCelsius;

Constants are all caps by tradition (C, C++)

Static variables in C, C++ are different than in Java 3

When to Use Static Methods

For methods that use only their arguments and

thus don t need an object for member data public static double pow(double b, double p) // Math library, takes b to the p power

For methods that only need static data fields

public static int getID( ) { return nextID++;} // nextID is a static variable (see previous page)

Main method in the class that starts the program

No objects exist yet for it to operate on

All methods in C are like static Java methods,

since C has no classes/objects

C++ has both Java-like and C-like methods

Exercise

Well experiment with whether rail locomotives

have enough power to haul a train at a given velocity Force Resistance: static friction, rolling friction, air

Decreases Increases with velocity

with velocity

Locomotive

Locomotive force limited All cars alike (same mass) by horsepower, adhesion 4

Exercise

Declare a class Train (Eclipse: File->New->Class)

Create one public constant: gravity g= 9.8

Youll finish this class later

Declare a class Engine (Eclipse: File->New->Class) Variables: (there can be many engines w/diff mass, power) Mass Power Coefficient of friction mu (0.3), a public constant for all engines Constructor, as usual. How many arguments does it have? getMass() method getForce() method with one argument, velocity f1= power/velocity (limit of engine horsepower) f2= mass * g * mu (limit of adhesion to rail) Return the minimum of f1, f2 (use Math.min() method)

Save / compile

Exercise, p.2

Write a static version of getForce() in class Engine

Supply all needed variables as arguments

Used by other classes that dont want to create an

Engine object

Method overloading:

We can have multiple methods with the same name as long as they take a different number and or type of arguments. We cannot have two methods that differ only in return type Overloading is general; its not related to static vs instance

To write this method:

First, copy and paste the instance version of getForce() you just wrote

Then modify it into the static version

You will need both versions of getForce() in this class 5

Exercise, p.3

Write class Car (Eclipse: File->New->Class)

Two private variables:

A single average mass for any car

Car type (coach, snack, first-class)

Constructor. How many arguments does it have?

Set and get methods for the single car mass

If you have time:

Write set and get methods for the car type

Are these instance or static methods?

Exercise, p. 4

Finish class Train

Data members:

Gravity g (already defined)

Constant c1= 0.00015 (rolling resistance)

Constant c2= 110.0 (air resistance)

One engine (object)

Number of cars (int)

(Which data members are static?)

Constructor

What variables does it set?

Method getNetForce(), with one argument: velocity

Compute weight= g*(engine mass + no of cars * car mass) Compute net force= engine force - c1*weight*v - c2*v*v

Return net force

6

Exercise, p.5

Download TrainTest and add one line to it:

blic class TrainTest { public static void main(String[] args) { double vel= 30.0; // 30 m/s, 70mph // Static method. No object needed. double f34= Engine.getForce(vel, 90000, 5500000); // Engine: 90 tonnes, 5500 kw

Engine r34= new Engine(90000, 5500000);

// Instance method double force34= r34.getForce(vel); // Don"t need to create Cars. All we need is their mass // But we must set their mass: // Set it to 50000 kg here // Train

Train amtrak41= new Train(r34, 10);

// Instance method double force41= amtrak41.getNetForce(vel); // Output (run TrainTest) ppuquotesdbs_dbs14.pdfusesText_20