Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Java 9 with JShell

You're reading from   Java 9 with JShell Introducing the full range of Java 9's new features via JShell

Arrow left icon
Product type Paperback
Published in Mar 2017
Publisher Packt
ISBN-13 9781787282841
Length 408 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Gaston C. Hillar Gaston C. Hillar
Author Profile Icon Gaston C. Hillar
Gaston C. Hillar
Arrow right icon
View More author details
Toc

Table of Contents (23) Chapters Close

Java 9 with JShell
Credits
About the Author
Acknowledgement
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
1. JShell – A Read-Evaluate-Print-Loop for Java 9 FREE CHAPTER 2. Real-World Objects to UML Diagrams and Java 9 via JShell 3. Classes and Instances 4. Encapsulation of Data 5. Mutable and Immutable Classes 6. Inheritance, Abstraction, Extension, and Specialization 7. Members Inheritance and Polymorphism 8. Contract Programming with Interfaces 9. Advanced Contract Programming with Interfaces 10. Maximization of Code Reuse with Generics 11. Advanced Generics 12. Object-Oriented, Functional Programming, and Lambda Expressions 13. Modularity in Java 9 Exercise Answers Index

Using static fields to provide class-level values


Sometimes, all the members of a class share the same attribute, and we don't need to have a specific value for each instance. For example, virtual creature types have the following profile values:

  • Attack power

  • Defense power

  • Special attack power

  • Special defense power

  • Average speed

  • Catch rate

  • Growth rate

A first approach we might think useful for this situation is to define the following class constants to store the values that are shared by all the instances:

  • ATTACK_POWER

  • DEFENSE_POWER

  • SPECIAL_ATTACK_POWER

  • SPECIAL_DEFENSE_POWER

  • AVERAGE_SPEED

  • CATCH_RATE

  • GROWTH_RATE

Note

Note the usage of uppercase and words separated by underscores (_) for class constant names. This is a naming convention in Java 9.

The following lines show a new version of the VirtualCreature class that defines the seven previously listed class constants with the public access modifier. Notice that the combination of the final and static keywords makes them class constants. The...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime
Visually different images