Java 17 Recipes


Download 3.2 Mb.
Pdf ko'rish
bet16/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   12   13   14   15   16   17   18   19   ...   245
Bog'liq
Java 17 Recipes

 Packages
The solution example begins by creating a Java package.
package org.java17recipes.chapter01.recipe01_05;
Packages are a way of grouping related classes together into a shared namespace. 
The idea is to achieve universal uniqueness by working your way down your 
organization’s domain name in reverse order. It is also customary to write package 
names in all lowercase.
Eclipse creates a directory structure to imitate your package path. In this case, 
Eclipse created the following directory path.
C:\eclipse-workspace\java17Recipes\src\org\java17recipes\chapter01\
recipe01_05
The following are some things to notice about this path.
• The front part is C:\eclipse-workspace. Eclipse creates all projects 
under a workspace directory that you can change. Developers specify 
different paths.
• Next is the first occurrence of java17Recipes. This corresponds to 
the project name (see Figure 
1-10
).
• Any source files you create go into the src directory. Eclipse creates 
other directories at this level. For example, Eclipse creates a bin 
directory, and then underneath it is a classes subdirectory to hold 
your compiled class files.
• The directories mirroring the package path that you specify are 
last, in this case, org\java17recipes\chapter01\recipe01_05. An 
identical structure is created under the bin\classes directory when 
you compile your code. Note that if using another IDE, you may see 
differences in the directories that are created.
ChApteR 1 GettInG StARted wIth JAvA 17


21
You need to explicitly create a package. Organization and a judiciously chosen 
naming convention are important when developing any significant application.
 JavaBeans-style Classes
Next in the solution example is a class definition following the JavaBeans pattern. 
The definition of HelloMessage follows a pattern that you often encounter in Java 
programming, and we include it for that reason. The class is simple, capable of holding a 
single string field named message.
Three methods are defined in the class.
• HelloMessage(). This method, also known as the constructor, is 
named the same as the class. In this case, it takes no arguments. 
It’s automatically invoked whenever you create a new object of the 
class. Note that this is known as a no-arg or no-argument constructor 
because it is typed out within the class and takes no arguments. If 
you do not supply a constructor, the JVM supply a default constructor 
(also takes no arguments) automatically.
• setMessage(String). This accessor method begins with the word 
set. It takes one parameter. It specifies the message to be returned by 
the corresponding get method.
• getMessage(). This accessor method returns the currently defined 
message. In this example, we chose to make the message uppercase.
Note Accessor methods are used in JavaBeans classes to access any privately 
declared class members. In this case, the private variable identified as message 
can be accessed using these methods. Accessor methods are more commonly 
referred to as getters and setters.
Methods beginning with set and get are the setter and getter methods. The IDE 
generates these methods. The message variable is private to the class, which means you 
have no direct access to message from outside the class.
You see the this keyword used in the class. It is a special keyword in Java that 
references the current object. Its use is redundant but would be needed if any of the 
methods happened to create variables of their own that were also named message: the 
ChApteR 1 GettInG StARted wIth JAvA 17


22
setter is mandatory due to the parameter name. It is common practice to use the this 
keyword to reference the class members from within the getter and setter methods, 
mostly for the latter.
It is common in Java to mediate access to class variables through setter and getter 
methods like in our example. Those methods represent a contract of sorts with other 
classes and your main program: public methods let the class state to be read or written, 
but the external code is unaware of the mapping. Their benefit is that you can change 
the storage implementation of HelloMessage however you like. It has its fields and state 
information saved and restored independently from the type of storage. The other code 
that depends on HelloMessage continues to work properly so long as you maintain the 
external behavior of setMessage() and getMessage().

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   12   13   14   15   16   17   18   19   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling