



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




ESE
ย
ย
ย
ESE
ย
ย
ย
ย^
If you scroll down the lower left panel and click onthe link labeled
Random
, the large "main" panel on
the right will display the documentation for theRandom class
ESE
ย
ย
ย^
E.g. Random r = new Random();
ย
ย^
ย^
0 (inclusive) and 6 (exclusive)
ย^
How do I generate a number between 1 and 6?
ESE
ย^
What is a
package
ย^
Basically it's a directory that has a collection of related classes ย^
E.g. Random Class description contains:
java.util.Random
ย^
Indicating that the Random class code is stored in the directorypath
java/util/
somewhere on your machine
-^
"util", or utility
package
ย^
Inorder to use implemented work, need to tell Java compilerwhere class is located^ ย
Use
import
statement
-^
import java.util.Random;
-^
Another way is to use the asterisk "wildcard character": importjava.util.*;
ESE
ย
ย
ย
ESE
ย^
Notice the phrase
java.lang
at the top of the main panel
above the word Math^ ย
This means that the Math class is part of the core Javalanguage and hence can be used directly
Math.PI3.141592653589793> Math.E2.718281828459045> Math.sqrt(25)5.0> Math.pow(2,10) 1024 > Math.cos(0)1.0> Math.cos(2 * Math.PI)1.
ESE
public class Math{
public static final double PI = 3.141592653589793;
public static double sin(double d){ .. }public static double sqrt(double d) { .. }.. } > Math.PI3.141592653589793> Math.sqrt(25)5.
ESE
ย^
A method may be declared with the
static
keyword
ย^
Static methods live at
class level
, not at
object level
ย^
Static methods can
access
static variables and
methods, but not dynamic ones^ ย
How could it choose which one? We have not created anyobjects yet ย^
Example:public static int getNumSold(){
return numTicketsSold; }
ESE
ย
ย^
ClassName.methodName(args) ย^
double result = Math.sqrt(25.0); ย^
int sold = Ticket.getNumberSold(); ย^
boolean b = isHappy(10);
ย
ย^
Using
ClassName.variableName
Static variables act as global variable i.e. accessiblewithin any static method
ESE
> Ticket.getNumberSold() 0 > Ticket t1 = new Ticket();> t1.getTicketNum() 1 > t1.getInfo()"ticket # 1; 1 ticket(s) sold."> t1.getNumberSold() 1 > Ticket t2 = new Ticket();> t2.getTicketNum() 2 > t2.getInfo()"ticket # 2; 2 ticket(s) sold."> t1.getInfo()"ticket # 1; 2 ticket(s) sold."> Ticket.getNumberSold() 2
ESE
ย
ย
ย^
Hence it is static i.e. does not belong to any object
ย
ESE
public class Point {
int x;int y;public static void main(String args[]) {
x = 5;y = 10; } }
all are wrong
ESE
public class Point {
int x;int y;public void setX(int val) {
x = 5; } โฆ.public static void main(String args[]) {
Point p = new Point();p.setX(5);โฆ
} }
Remember that if no constructoris written, java creates a defaultconstructor and initializes theinstance variables to their defaultvalues
ESE
ย
ย
ย
ESE
ย^
static
variables and methods belong to the class in general, not to individual objects
ย^
The absence
of the keyword
static
before non-local variables
and methods means
dynamic
(one per object/instance)
ย^
A dynamic method can access all dynamic
and
static variables
and methods in the same class
ย^
A static method can not access a dynamic variable
(How could
it choose or which one?)
ย^
A static method can not call a dynamic method (becausedynamic method might access an instance variable)