Java
/
OOP
- 1 Basics 9
-
Classes S
-
Objects S
-
Arrays S
-
Variables S
-
Loops S
-
Numbers S
-
Strings S
-
Exceptions S
-
Regexp S
- 2 OOP 9
-
Inheritance
-
Polymorphism
-
Static S
-
Abstract
-
Interfaces
-
Constructors S
-
Packages
-
Nested Classes
-
Final
- 3 Compiler 2
-
Sublime Text S
-
Apache Ant
- 4 Collections 8
-
Lists
-
Comparable S
-
Sets
-
Maps
-
Generics
-
Properties
-
Streams
-
Json
- 5 Threads 4
-
Create Thread S
-
Sleep
-
Lock
-
Scheduler
- 6 Design Patterns 4
-
Singleton
-
Observer
-
Strategy
-
Mediator
- 7 Swing 12
-
Frame
-
Panel
-
Listener
-
Combo Box
-
Label
-
Image
-
Menu
-
Table
-
Layout
-
Drawing
-
Timer
-
Designer
- 8 I/O 7
-
Streams IO
-
Socket
-
Watching Files
-
Mail
-
Logger
-
Clipboard
-
Encrypt S
- 9 Effective 7
-
Constructors S
-
Dependency Injection
-
Composition
-
Interfaces Default
-
Import Static S
-
Enums
-
Lambdas
- 10 Junit 5
-
About Junit S
-
Test Case
-
Suite Test
-
Annotations
-
Exceptions
- 11 Lambdas 7
-
Expressions S
-
Functional Interfaces
-
Streams
-
Common Operations
-
Default Methods
-
Static Methods S
-
Single Responsibility
- 12 JavaFX 6
-
Openjfx
-
Scene Builder
-
First App
-
Jar Archive
-
On Action
-
Change Listener
- 13 Maven 4
-
Demo
-
Spring Boot
-
Junit
-
Guava
- 14 Spring Boot 13
-
Quick start S
-
Rest service S
-
Consuming rest S
-
Templates S
-
Security auth S
-
Command line S
-
Scheduled task S
-
Ajax S
-
Jdbc mysql S
-
Encrypt password S
-
Https S
-
Jwt S
-
Post request S
S
R
Q
Java OOP Static Keyword
Static variables, initialized only once Methods, belongs to class, not to instance static int count; static long myRound(Double n) return Math.round(n)Static
Variables
p279 Static variables are initialized only once.
/**
* Static variables are initialized only once.
*
* A static class variable is common to all objects created with that class.
* It can be called without object instantiation.
*/
package com.minte9.oop.static_keyword;
public class Variables {
public static void main(String[] args) {
A a1 = new A();
A a2 = new A();
System.out.println(a1.getCount()); // 2 (not 1)
System.out.println(a2.getCount()); // 2
}
}
class A {
private static int count;
public A() {
count++;
}
public int getCount() {
return count;
}
}
Methods
p275 Static methods can't be access from a class instances.
/**
* Static methods belong to the class and not every instance of the class.
* Example: Java.lang.Math has a round() static method.
*/
package com.minte9.oop.static_keyword;
public class Methods {
public static void main(String[] args) {
System.out.println(
Math.round(42.2) // 42
);
System.out.println(
Methods.myRound(33.5) // 34
);
// Math m = new Math(); // Error: private access
}
private static long myRound(Double n) {
return Math.round(n);
}
}
Classes
Only nested classes can be static.
/**
* Static classes
*
* Only nested classes can be static
* You can use the nested class without an instance for outer class
*/
package com.minte9.oop.static_keyword;
public class Classes {
static int a = 10;
public static void main(String[] args) {
InnerClass.run(); // 10
}
static class InnerClass {
public static void run() {
System.out.println(a); // field from Outer Class
}
}
}
➥ Questions
Last update: 121 days ago