From ab73c5012ba698bbbd2dcb4514340a16f2bc8df1 Mon Sep 17 00:00:00 2001 From: Sam Hos Date: Wed, 15 May 2024 17:03:02 +0200 Subject: [PATCH] java class explanation --- .../android/Java class modifiers.md | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 docs/documentation/android/Java class modifiers.md diff --git a/docs/documentation/android/Java class modifiers.md b/docs/documentation/android/Java class modifiers.md new file mode 100644 index 0000000..0ff6895 --- /dev/null +++ b/docs/documentation/android/Java class modifiers.md @@ -0,0 +1,109 @@ +# Java class modifiers + +## How are classes build in java? +There are a lot of classes with a lot of parameters + +A class always begins with either the public or default or abstract access modifier. Then comes the class name + +Example: +```java +public class MyClass { + //methods and atributes +} + +``` +Then in a class you can have Methods or Atributes where the actual code is. + +```mermaid +flowchart +a[Access Modifiers] --> b[class] --> c[Class name] +``` + +## Constructor +A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes. + +The constructor Method is the first method that is called when a class is created. It has the same name as the class and no return type. It is automaticly called when a class is created. + +### Methods +A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain actions, and they are also known as functions. + +Methods are build like this +```mermaid +flowchart +A[Access Modifiers] --> B[Method Modifier] --> C[Return Type] --> D[Method Name] --> E[Parameters] --> F[Method Body] +``` + + +Example: +```java +public class MyClass { + // Method + static void myMethod() { + // print Hello World! + System.out.println("Hello World!"); + } + // Main method + //access modifier, Method Modifier, return type, method name, parameters, method body + public static void main(String[] args) { + // Call the method + myMethod(); + } +} +``` + + +### Atributes +Attributes are variables within a class. When an object is created from a class, the object will have these attributes. + +Example: +```java +public class MyClass { + int x = 5; +} +``` + +## Classes +### Class modifiers + +#### Non-access modifiers +| Modifier | Description | | | | +|---|---|---|---|---| +| final | The class cannot be inherited by other classes (You will learn more about inheritance in the Inheritance chapter) | | | | +| abstract | The class cannot be used to create objects (To access an abstract class, it must be inherited from another class. You will learn more about inheritance and abstraction in the Inheritance and Abstraction chapters) | | | | +|||| + +#### Access modifiers +| Modifier | Description | | | | +|---|---|---|---|---| +| public | The code is accessible for all classes | | | | +| private | The code is only accessible within the declared class | | | | +| default | The code is only accessible in the same package. This is used when you don't specify a modifier. You will learn more about packages in the Packages chapter | | | | +| protected | The code is accessible in the same package and subclasses. You will learn more about subclasses and superclasses in the Inheritance chapter | | | | +| | | | | | + + +## Methods and attributes +#### non access modifiers +| Modifier | Description | | | | +|---|---|---|---|---| +| final | Attributes and methods cannot be overridden/modified | | | | +| static | Attributes and methods belongs to the class, rather than an object. You don't really wanna use this one. | | | | +| abstract | Can only be used in an abstract class, and can only be used on methods. The method does not have a body, for example abstract void run();. The body is provided by the subclass (inherited from). You will learn more about inheritance and abstraction in the Inheritance and Abstraction chapters | | | | +| transient | Attributes and methods are skipped when serializing the object containing them | | | | +| synchronized | Methods can only be accessed by one thread at a time | | | | +| volatile | The value of an attribute is not cached thread-locally, and is always read from the "main memory" | | | | +|||| + +#### Access modifiers +| Modifier | Description | | | | +|---|---|---|---|---| +| public | The code is accessible for all classes | | | | +| private | The code is only accessible within the declared class | | | | +| default | The code is only accessible in the same package. This is used when you don't specify a modifier. You will learn more about packages in the Packages chapter | | | | +| protected | The code is accessible in the same package and subclasses. You will learn more about subclasses and superclasses in the Inheritance chapter | | | | +| | | | | | + + +## Sources + +* https://www.w3schools.com/java/java_modifiers.asp