From ecc4fa823c35a48c37b942646f8bff208430882b Mon Sep 17 00:00:00 2001 From: Arghya Ghosh Date: Mon, 5 Aug 2024 07:46:20 +0530 Subject: [PATCH 1/5] feat(docs): Create static-keyword --- src/pages/docs/static-keyword | 36 +++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/pages/docs/static-keyword diff --git a/src/pages/docs/static-keyword b/src/pages/docs/static-keyword new file mode 100644 index 00000000..ea366933 --- /dev/null +++ b/src/pages/docs/static-keyword @@ -0,0 +1,36 @@ +--- +title: Java static keyword +description: In this tutorial, we will learn how to use the Java static keyword. +--- + +What is a `static` keyword in Java? + +In Java, if we want to access class members, we must first create an instance of the class. But there will be situations where we want to access class members without creating any variables. +In those situations, we can use the `static` keyword in Java. If we want to access class members without creating an instance of the class, we need to declare the class members static. +The `Math` class in Java has almost all of its members static. So, we can access its members without creating instances of the Math class. + +### Example 1 + +```java +public class Main { + public static void main( String[] args ) { + + // accessing the methods of the Math class + System.out.println("Absolute value of -12 = " + Math.abs(-12)); + System.out.println("Value of PI = " + Math.PI); + System.out.println("Value of E = " + Math.E); + System.out.println("2^2 = " + Math.pow(2,2)); + } +} +``` +### Output: + +```bash +Absolute value of -12 = 12 +Value of PI = 3.141592653589793 +Value of E = 2.718281828459045 +2^2 = 4.0 +``` +In the above example, we have not created any instances of the Math class. But we are able to access its methods: `abs()` and `pow()` and variables: `PI` and `E`. + +It is possible because the methods and variables of the Math class are static. From fcee95d1c69f675b71b8a9386a9bbec0a067de8b Mon Sep 17 00:00:00 2001 From: Arghya Ghosh Date: Mon, 5 Aug 2024 07:48:43 +0530 Subject: [PATCH 2/5] feat(nav): Update docs nav --- src/navs/documentation.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/navs/documentation.js b/src/navs/documentation.js index 980e2103..f447c340 100644 --- a/src/navs/documentation.js +++ b/src/navs/documentation.js @@ -28,4 +28,5 @@ export const documentationNav = { pages['continue-statement'], ], 'Java Arrays': [pages['arrays']], + 'Java OOP(I)': [pages['static-keyword']], } From 090a921dc8f3a510b3d18fa81fcfcb7d2f96027e Mon Sep 17 00:00:00 2001 From: Arghya Ghosh Date: Mon, 5 Aug 2024 07:52:40 +0530 Subject: [PATCH 3/5] feat(nav): Update docs navs --- src/navs/documentation.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/navs/documentation.js b/src/navs/documentation.js index f447c340..a25d09e5 100644 --- a/src/navs/documentation.js +++ b/src/navs/documentation.js @@ -28,5 +28,5 @@ export const documentationNav = { pages['continue-statement'], ], 'Java Arrays': [pages['arrays']], - 'Java OOP(I)': [pages['static-keyword']], + 'Java OOPS': [pages['static-keyword']], } From 3871b1e1bae6c8074637bdd3cea5288070de3b4f Mon Sep 17 00:00:00 2001 From: Arghya Ghosh Date: Mon, 5 Aug 2024 07:58:34 +0530 Subject: [PATCH 4/5] Rename static-keyword to static-keyword.mdx --- src/pages/docs/{static-keyword => static-keyword.mdx} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/pages/docs/{static-keyword => static-keyword.mdx} (100%) diff --git a/src/pages/docs/static-keyword b/src/pages/docs/static-keyword.mdx similarity index 100% rename from src/pages/docs/static-keyword rename to src/pages/docs/static-keyword.mdx From ea4ee668750ba69d1abd74605a0ae64bd2630825 Mon Sep 17 00:00:00 2001 From: Arghya Ghosh Date: Mon, 5 Aug 2024 07:58:48 +0530 Subject: [PATCH 5/5] Update static-keyword.mdx --- src/pages/docs/static-keyword.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/docs/static-keyword.mdx b/src/pages/docs/static-keyword.mdx index ea366933..ee43672a 100644 --- a/src/pages/docs/static-keyword.mdx +++ b/src/pages/docs/static-keyword.mdx @@ -3,7 +3,7 @@ title: Java static keyword description: In this tutorial, we will learn how to use the Java static keyword. --- -What is a `static` keyword in Java? +## What is a `static` keyword in Java? In Java, if we want to access class members, we must first create an instance of the class. But there will be situations where we want to access class members without creating any variables. In those situations, we can use the `static` keyword in Java. If we want to access class members without creating an instance of the class, we need to declare the class members static.