{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Static - Examples • All the methods in the Math class...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Static Methods and Static Fields Classes can have static methods and static fields. Static fields do not change from one instance of a class to another, so you should think of them as belonging to a class. Similarly static methods belong to a class and do not operate on any instance of a class. This means that you can use them without creating an instance of a class. How to use them: You can use static and non-static fields and methods in any class. The syntax for a static method from a class: ClassName.staticMethod(parameters); We use the class name when invoking a static method. The syntax for a static field is: ClassName.staticFieldName
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Examples: • All the methods in the Math class built into Java are static methods: • Math class is in the java.lang package. double x = Math.pow(4, 0.5); // sqrt root of 4 int y = Math.max(3, 5); // maximum of 3 , 5 • Some of the static fields in Math : Math.PI; //public static final double PI Math.E; //public static final double E Hold down your control key + C key to follow the link to Java Platform Standard Ed. 6 and scroll down the lower left hand pane to find the Math class and click on it for more methods in that class. Overview (Java Platform SE 6) http://java.sun.com/javase/6/docs/api/...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online