day-2

Day 2

Access Modifiers

Used to set the access level for classes, attributes, methods and constructors.

  • Default

String name = "Amigoscode"
  • Private Only accessible inside same class

private String name = "Amigoscode"
  • Public The code is accessible for all classes

public String name = "Amigoscode"
  • Protected Accessible in the same package and subclasses.

protected String name = "Amigoscode"

Methods

A method is a block of code which only runs when invoked using (). We can use methods to reuse code that can be shared across the entire application.

Methods are composed of

  • Access Modifier

  • Return type

  • Name

  • Optional arguments

  • Method body

  • Optional return value

For example

Enums

An Enum is a type used specifically to represent sets of constants. For example, MALE or FEMALE.

To create an Enum is as following:

Working with Dates

Java has a great API for working with dates. You should be aware of these classes:

  • LocalDate

  • LocalDateTime

  • LocalTime

Explore the methods available for each classes.

Error Handling

When building applications things can go wrong and it is best practice to handle scenarios where our code does not behave as expected. Or simply deal with errors that we can anticipate.

I java we errors are called Exceptions and we can deal with them using the following constructs:

  • The try statement allows you to define a block of code to be tested for errors while it is being executed.

  • The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

Checked Exceptions

Checked exceptions represent errors outside the control of the program. For example reading a file. You need to handle this error during compile time otherwise your code will not compile.

Unchecked Exceptions

If a program throws an unchecked exception, it reflects some error inside the program logic. For example, if we divide a number by 0, Java will throw ArithmeticException:

Working with files

Last updated