From the course: Oracle Java Certification: 3. Methods and Inheritance

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Static methods and fields

Static methods and fields - Java Tutorial

From the course: Oracle Java Certification: 3. Methods and Inheritance

Start my 1-month free trial

Static methods and fields

- [Instructor] In a Java class member methods and fields can be defined as static. Static members belong to the class, and only a single copy of each static member will be associated with a class object. This has some significant implications on how static members can be used. For instance, a static member is always executed in a static context, and therefore, it does not have access to any instance members of the class. So it doesn't make sense to call a member method in a static method of the same class. Let's look at an example. This program defines a static print method in line two and invokes the method in several different ways in the main method. What do you think this program would output? The first score at line eight should work. We should be able to invoke a static method where an object reference. At line nine, the print method is called a directly. It should also work because the call is made in the static…

Contents