0% found this document useful (0 votes)
3 views21 pages

Presentation Number 2

Java concepts

Uploaded by

alfredbanda847
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views21 pages

Presentation Number 2

Java concepts

Uploaded by

alfredbanda847
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Programming in Java

by: Chimango Nyasulu, PhD


Outline
Program Layout, Naming, Comments
Class/Object Structure
Java Library Packages
Program Layout
Following a consistent style guide can greatly enhance the readability and maintainability of your code.

1. File Structure: Each class should be in its own file, named after the class (e.g., MyClass.java for a
class named MyClass).

▪ Use packages to organize related classes.

2. Indentation: Use 4 spaces for indentation. Avoid using tabs as spaces can be displayed differently
on different editors.

3. Line Length: Keep lines to a maximum of 80-120 characters. If a line exceeds this length, consider
breaking it into multiple lines.
Program Layout
4. Braces: Use K&R style for braces: place the opening brace on the same line as the control
statement or method declaration.

5. Whitespace: Use blank lines to separate logical sections of code, such as between method
definitions or groups of related methods. Use a single blank line to separate statements within a
method.

6. Ordering of Methods: Place public methods before private methods. Group related methods
together (e.g., getters and setters).
Naming Conventions
1. Class Names: In Java, the naming convention for class names follows a set of standard practices
that help maintain consistency and readability in code.

▪ Class names should be written in Pascal case (Upper Camel Case), where the first letter of each
word is capitalized, and there are no underscores or spaces. For example: CustomerAccount.

▪ Class names should be descriptive and convey the purpose of the class. For example: Instead of
Calc, use Calculator.

▪ Avoid using abbreviations unless they are widely accepted (like HTML, URL, etc.). Using full words
makes the code more readable.

▪ Since classes typically represent objects or entities, class names should generally be nouns. For
example: Car, Invoice, UserProfile.
Naming Conventions
2. Method Names: In Java, method names follow specific naming conventions that help improve code
readability and maintainability.

▪ Method names should start with a lowercase letter and use camel case for subsequent words. For
example: calculateTotal(), getUserName().

▪ Method names should be descriptive and indicate the action that the method performs. For
example: findUserById(int id).

▪ It's often a good practice to use a verb followed by a noun to clearly indicate the action being
performed. For example: addItemToCart().

▪ When parameters are used in methods, the names of the parameters should also follow the camel
case convention and be descriptive.
Naming Conventions
3. Variable Names: In Java, following a consistent naming convention for variable names is crucial for
code readability and maintainability.

▪ Use lower camel case for variable names, which means the first word is in lowercase, and
subsequent words start with an uppercase letter. For example: myVariable, totalAmount.

▪ Variable names should be descriptive enough to convey the meaning or purpose of the variable. For
example: Use customerAge instead of age.

▪ Except for loop counters or very temporary variables, avoid single-letter names.

▪ Do not use keywords reserved by the Java language as variable names (e.g., int, class, public).

▪ Java is case-sensitive, so myVariable, MyVariable, and MYVARIABLE would be considered different


identifiers.
Naming Conventions
4. Constant Names: In Java, the naming convention for constants is typically to use all uppercase
letters with words separated by underscores. This style helps distinguish constants from variables
and makes them easily recognizable.

▪ Use descriptive names for constants that convey their purpose or meaning.
Naming Conventions
5. Package Names: In Java, naming conventions for package names are important for maintaining
readability and avoiding conflicts.

▪ Package names should be written in all lowercase letters to avoid conflicts with class names. This
helps distinguish between classes and packages.

▪ It is common practice to use a reverse domain name as the base of the package name. For example,
if your domain is example.com, you would start your package names with com.example.

▪ Package names should reflect the hierarchy of the project. Sub-packages can be created using dots
(.) to separate different levels. For instance, com.example.project.module.

▪ Choose meaningful names for packages that describe their contents or functionality.
Comments
▪ Comments are used to explain code, making it easier to understand for anyone reading it later,
including the original author.

▪ Comments are ignored by the Java compiler, meaning they do not affect the execution of the
program.

There are three main types of comments in Java:

1. Single-line Comments: Single-line comments begin with // and continue until the end of the line.
Comments
There are three main types of comments in Java:

2. Multi-line Comments: Multi-line comments begin with /* and end with */. They can span multiple
lines and are useful for longer explanations or for commenting out blocks of code.
Comments
There are three main types of comments in Java:

3. Javadoc Comments: Javadoc comments begin with /** and end with */. These comments are
specifically used to create documentation for classes, methods, and fields.
Class Structure
1. Use appropriate visibility modifiers (public, protected, private, or package-private) to encapsulate
your classes and their members properly.

▪ When a class, method, or variable is declared as public, it can be accessed from any other class in
any package.
▪ The protected modifier allows members to be accessible in the following contexts:

• Any class that is in the same package as the class declaring the protected member can access
that member.

• Any subclass (even if it is in a different package) can access the protected members of its
superclass.
Class Structure
▪ The private visibility modifier is used to restrict access to the members (fields, methods, and inner
classes) of a class.

▪ When a member is declared as private, it can only be accessed within the same class where it
is defined.

▪ The package-private visibility modifier (also known as "default" visibility) is the access level that is
applied to classes, methods, and variables when no explicit access modifier is specified.

▪ It allows members to be accessible only within the same package.


Class Structure
2. Member variables (also known as instance variables or fields) are variables that are declared within
a class but outside any methods or constructors.

▪ These variables are used to store the state or attributes of an object created from that class.

▪ Are accessible to all methods within the class, and they can also be accessed by methods in
other classes if they are declared with appropriate access modifiers (like public, protected, or
private).

▪ The lifetime of a member variable is tied to the lifecycle of the object it belongs to. They are
created when an object is instantiated and destroyed when the object is no longer referenced.

▪ If a member variable is not explicitly initialized, it is assigned a default value based on its type.
Class Structure
Class Structure
3. A constructor is a special type of method that is used to initialize objects. It is called when an
instance of a class is created, and it has the same name as the class.

▪ The constructor must have the same name as the class.

▪ Constructors do not have a return type, not even void.

▪ Constructors are automatically called when an object is created using the new keyword.

▪ You can have multiple constructors in a class with different parameter lists (constructor
overloading).

▪ Default Constructor: A constructor that does not take any parameters.

▪ Parameterized Constructor: A constructor that takes parameters to initialize an object with


specific values.
Class Structure
Class Structure
4. Methods are blocks of code that perform a specific task. They are defined within a class and are
used to execute specific actions, manipulate data, or return values.

▪ Key components of a method are Access Modifier, Return Type, Method Name, Parameters,
and Method Body.
Class Structure
Java Library Packages
Java is organized into a collection of libraries known as packages, which group related classes and
interfaces together. For example:

1. java.lang: This package is automatically imported into every Java program.

▪ It contains fundamental classes such as String, Math, Integer, System, Object, and Thread.

2. java.util: Contains utility classes that are widely used, including:

▪ Collections framework (e.g., List, Set, Map, Queue)

▪ Date and time facilities (Date, Calendar, TimeZone)

▪ Random number generation (Random)

You might also like