CTIS 222 Week 8 Review On OO and Java
CTIS 222 Week 8 Review On OO and Java
• Multi-tasking • Multi-threading
• Strong-typed • Loose-typed
int x; var x;
OR
$x;
For More Info: "The C programming language" Kernighan, Brian W.
Parameters vs. Arguments
• Q: What is the difference between a parameter
and an argument?
• A:
Parameter
Argument
function addTwoNumbers(a,b){ ...
... addTwoNumbers(5,7);
//function code goes here
...
...
}
Source: Foundations of Programming (3 courses)\1 - Fundamentals\1 - Programming Basics
Memory Management
• Q: What is memory leakage? And what is
dangling reference?
• A:
• Memory Leakage
– When you forget to release the unused object(s).
• Dangling Reference
– When you attempt to reach the object you already
released.
Memory Management
Source: Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design 2008
Visibilities…
public static void main(String[] args)
{
//statements go here
}
Source: Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design 2008
Valid vs. Invalid – Why/not?
String word;
word = System.out.println("Hello World");
• Invalid.
• The method println is a void method and cannot appear in
an assignment statement.
Source: Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design 2008
Valid vs. Invalid – Why/not?
Source: Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design 2008
Categories of variables
• In Java, there are 4 categories of variables.
– instance variables
declared at class level
– static variables
– parameter variables
declared at block level
– local variables
• A block is a sequence of statements enclosed within a pair of
braces { and }.
Source: Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design 2008
Categories of variables
char nextCharacter;
int totalGamesPlayed;
double stateTaxRate = 6.85;
String nextLine = "Have a close look at me!";
Source: Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design 2008
Categories of variables
char nextCharacter;
int totalGamesPlayed;
double stateTaxRate = 6.85;
String nextLine = "Have a close look at me!";
Source: Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design 2008
Categories of variables
char nextCharacter;
int totalGamesPlayed;
double stateTaxRate = 6.85;
String nextLine = "Have a close look at me!";
Source: Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design 2008
Categories of variables
char nextCharacter;
int totalGamesPlayed;
double stateTaxRate = 6.85;
String nextLine = "Have a close look at me!";
• This statement is quite different from the other three statements.
• This is due to the fact that String is a class in Java and not a primitive data type.
In the case of classes, the only variables you can declare are reference variables.
• Now a reference variable is one that can keep the reference of an object.
• A memory location is allocated and labeled as nextLine. However, memory
location nextLine does not contain the String "Have a close look at me!".
Instead, the String is stored somewhere else in memory and the reference of the
String is kept in the reference variable nextLine.
Source: Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design 2008
Categories of variables
char nextCharacter;
int totalGamesPlayed;
double stateTaxRate = 6.85;
String nextLine = "Have a close look at me!";
Source: Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design 2008
Scope of variables
• An instance variable is available only within the
object (if it is declared private).
• A local variable is available only within the block
from its point of declaration.
• A formal parameter is available within the
method.
Source: Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design 2008
Scope of variables
• formal parameter — the identifier used in a
method to stand for the value that is passed
into the method by a caller.
• amount is a formal parameter of pDeposit
• Method level:
final float ERROR_ALLOWED = 0.01F;
final double POUND_TO_KILOGRAM = 0.454;
Source: Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design 2008
Named constant
[public][static] final dataType IDENTIFIER = literal;
final dataType IDENTIFIER = literal;
Source: Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design 2008
System.out.println("Hello World");
Source :Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design, 2008
Accessor methods
• Here, we provide the accessor methods that
return the value of an instance variable of the
Stock class.
public double yearlyDividend()
{
double totalDividend;
totalDividend = numberOfShares * dividend;
return totalDividend;
class Stock
} {
private int numberOfShares;
private String tickerSymbol;
private double dividend;
}
Source :Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design, 2008
Mutator methods (a.k.a. setter methods)
• A method that changes the value of one or more instance
variables is called a mutator method.
• Here, we develop a mutator methods of the Stock class
to set the number of shares.
public void setNumberOfShares(int inNumberOfShares)
{
numberOfShares = inNumberOfShares;
}
class Stock
{
private int numberOfShares;
private String tickerSymbol;
private double dividend;
}
Source :Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design, 2008
toString METHOD
The toString method returns a String that contains essential
information. Observe that toString is an accessor method that returns a
String.
Ex: We assume that for a stock, the most essential information is the
number of shares currently owned and the stock symbol.
class Stock
{
private int numberOfShares;
private String tickerSymbol;
public String toString() private double dividend;
{ }
String str;
str = numberOfShares + " " + tickerSymbol;
return str;
}
Source :Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design, 2008
Object declaration and creation
Stock testStock = new Stock();
• The left-hand side of the above assignment
statement declares a reference variable
testStock of the type Stock.
• The right-hand side creates a new object
belonging to the Stock class and returns the
reference.
Source :Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design, 2008
Object declaration and creation
Stock testStock; //(2)
testStock = new Stock(); //(3)
• Statement 2 declares a local variable
testStock.
• Statement 3 creates a new instance of Stock
and initializes the reference variable
testStock with the reference of the newly
created instance returned by the new operator.
• In other words, Statement 3 instantiates the local
variable testStock.
Source :Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design, 2008
Method Invocation
• Here, we assume that all the methods
introduced are public, value returning, and
predefined.
1.static methods that have no parameters.
2.static methods that have parameters.
3. Methods that are not marked as static and
have no parameters.
4. Methods that are not marked as static and
have parameters.
Source :Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design, 2008
Method Invocation
• Here, we assume that all the methods
introduced are public, value returning, and
predefined.
1.static methods that have no parameters.
number = 1 + (int) (10 * Math.random());
Source :Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design, 2008
Method Invocation
• Here, we assume that all the methods
introduced are public, value returning, and
predefined.
1.static methods that have no parameters.
2.static methods that have parameters.
number = 10 * (int) Math.ceil(17.4999);
Source :Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design, 2008
Method Invocation
• Here, we assume that all the methods
introduced are public, value returning, and
predefined.
1.static methods that have no parameters.
2.static methods that have parameters.
3. Methods that are not marked as static and
have no parameters.
number = 10 * strOne.length();
Source :Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design, 2008
Method Invocation
• Here, we assume that all the methods
introduced are public, value returning, and
predefined.
1. static methods that have no parameters.
2. static methods that have parameters.
3. Methods that are not marked as static and
have no parameters.
4. Methods that are not marked as static and
have parameters.
ch = strOne.charAt(3);
Source :Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design, 2008
Method Overloading (a.k.a. static polymorphism)
• In Java, method name need NOT be unique.
• There can be many methods all having the
same name.
• The PL feature that allows the programmer to
create more than one method within a class
having the same name.
• public int indexOf(char ch)
• public int indexOf(String str)
• public int indexOf(char ch, int s)
• public int indexOf(String str, int s)
Source :Premchand S. Nair-Java Programming Fundamentals Problem Solving Through Object Oriented Analysis and Design, 2008
Q & A?
• Q: Can we have one class in another class in
Java?
• A: Yes.
class OuterClass {
...
static class StaticNestedClass {
...
}
class InnerClass {
...
}
}
Q & A?
public class A {
public static void main(String[] args) {
B myB = new B();
myB.run();
}
}
Saved as a
class B{ single Java file
int x; A.java
int y;
String z;
void run() {
System.out.println("I am running");
}
}
A: True
int x = 7;
String obj = "xyzt";
System.out.println(x);
System.out.println(obj);
}
}
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Primitive type in Java
Data Type Default Value (for fields)
1 byte 0
2 short 0
3 int 0
4 long 0L
5 float 0.0f
6 double 0.0d
7 char '\u0000'
8 boolean false
Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Primitive type in Java
• Java primitive types examples:
–int and double
• Adapters are sometimes called
wrappers.
• The Java wrapper classes Integer,
Float, Double etc. are adapters for
the Java primitive types.
Source: Object-Oriented Software Engineering Practical software development using UML and Java, 2nd Edt., Timothy C. Lethbridge and Robert Laganière, 2005
Variables of Primitive & Reference Types
• Every variable represents a memory location that
holds a value.
• When you declare a variable, you are telling the
compiler what type of value the variable can hold.
– For a variable of a primitive type, the value is of the
primitive type.
– For a variable of a reference type, the value is a
reference to where an object is located.
0X556677
Heap
0X35AADE
Triangle_obj Square_obj
0X556677 0X35AADE Stack
Stack
Source: Source: Udemy Master Object Oriented Design in Java - Homework + Solutions
Memory Locations
Memory
main(){
…
Stack
When you hit the
run button, the first
method invocation is First Frame main()
main()
Source: Source: Udemy Master Object Oriented Design in Java - Homework + Solutions
Memory Locations
Memory
main(){
int a;
Stack
When you hit the
run button, the first
method invocation is a ?
First Frame main()
main()
Source: Source: Udemy Master Object Oriented Design in Java - Homework + Solutions
Memory Locations
Memory
main(){
int a;
a=15;
Stack
When you hit the
run button, the first
method invocation is a 15
First Frame main()
main()
Source: Source: Udemy Master Object Oriented Design in Java - Homework + Solutions
Memory Locations
Memory
main(){
int a;
a=15;
DoMethod();
Source: Source: Udemy Master Object Oriented Design in Java - Homework + Solutions
Memory Locations
Memory
main(){
int a;
a=15;
DoMethod();
}
Source: Source: Udemy Master Object Oriented Design in Java - Homework + Solutions
Memory Locations
Memory
main(){
int a;
a=15;
DoMethod();
}
Source: Source: Udemy Master Object Oriented Design in Java - Homework + Solutions
Memory Locations
Memory
main(){
int a; Swim() method is removed
a=15; from the stack
DoMethod();
Third Frame
}
Second Frame DoMethod(); ? Stack
DoMethod() b
{ a 15
int b; First Frame main()
…
Swim();
When Swim() method is
executed completely
}
Source: Source: Udemy Master Object Oriented Design in Java - Homework + Solutions
Memory Locations
Memory
main(){
int a;
a=15; DoMethod() method is
DoMethod(); removed from the stack
}
Source: Source: Udemy Master Object Oriented Design in Java - Homework + Solutions
Memory Locations
Memory
main(){
int a;
a=15;
DoMethod();
…
Course myC = new Course();
Stack
}
a 15
First Frame main()
Source: Source: Udemy Master Object Oriented Design in Java - Homework + Solutions
Memory Locations
Memory
main(){
int a;
a=15;
DoMethod();
…
Course myC = new Course();
Stack
}
a 15
First Frame main()
myC XYZ
Source: Source: Udemy Master Object Oriented Design in Java - Homework + Solutions
Memory Locations – Instance Variable
Memory
// Outside the main
class A {
//instance variable
int xyz;
...
}
Stack
0xaadd A Heap
xyz
Source: Source: Udemy Master Object Oriented Design in Java - Homework + Solutions
Memory Locations – Instance Variable
Memory
// Outside the main
class A {
//instance variable
int xyz;
...
}
Stack
0xaadd A Heap
xyz 8
Source: Source: Udemy Master Object Oriented Design in Java - Homework + Solutions
Memory Locations – Instance Variable
Memory
// Outside the main
class A {
//instance variable Stack
main()
int xyz;
First Frame myA 0xaadd
...
myA2 0xbcdd
}
B
A myA = new A (); 0xeeff
myA.xyz = 8;
A xyz ?
A myA2 = new A ();
B myB1 = new B(); 0xbcdd myB Heap
?
0xaadd A xyz 8
myB ?
Source: Source: Udemy Master Object Oriented Design in Java - Homework + Solutions
Memory Locations – Instance Variable
Memory
// Outside the main
class A {
//instance variable Stack
main()
int xyz;
First Frame myA 0xaadd
...
B myB; myA2 0xbcdd
} myB1 0xeeff
B
A myA = new A (); 0xeeff
myA.xyz = 8;
A xyz ?
A myA2 = new A ();
B myB1 = new B(); 0xbcdd myB Heap
?
myA.myB = myB1;
0xaadd A xyz 8
myB 0xeeff
Source: Source: Udemy Master Object Oriented Design in Java - Homework + Solutions
this reference
• Objects are stored in the heap.
– Inside the objects: Only the variables are stored not
the methods.
• There is only one copy of the method. A method
is shared by all the objects.
– Method body Permanent Generation space of
heap
Int
public interface Printable { public interface Writable {
void print(); void write();
} }
Notable
<<interface>>
Main
Q & A?
• Q: Can an abstract class implements an
interface in Java?
• A: Yes.
Q & A?
• Q: Can an abstract class implements an
interface in Java? OUTPUT:
print in concrete class A
void m9();
abstract void m3();
void m4() {... } }
}
} }
Q & A?
• Q: Is it necessary that a given class must
implement ALL the methods declared in the
interfaces?
• A: Yes.
Q & A?
• Q: Can an interface extend a class in Java?
• A: No.
– If this was the case, a method’s body should have
implemented within that interface . This violates
the interface definition.
Q & A?
• Q: Can an interface contain a constructor in
Java?
• A: No.
Q & A?
• Q: Can you create an instance of an interface
in Java?
• A: No.
Q & A?
• Q: Can an interface contain instance fields
in Java?
• A: No.
Q & A?
• Q: Can an interface contain non-abstract
(concrete) methods in Java?
• A: No.
Q & A?
• Q: Can an interface be given the private
access modifier?
• A: No. Then the interface could never be
used
– All abstract, default, and static methods
in an interface are implicitly public, so you
can omit the public modifier.
• The Java compiler adds
– public and abstract keywords before the
interface method.
– public, static and final keywords before
data members.
Q & A?
• Q: Can an interface contain the following
type of members in Java?
– public, static, final fields (i.e., constants)
– default and static methods with bodies
• A: Yes.
Common Uses of interfaces
• Interfaces are flexible things and can be used for
many purposes in Java:
– Specifying minimal functional requirements for classes
(This is its principal purpose.)
– A work-around for Java’s lack of multiple inheritance.
– For defining groups of related symbolic constants.
(This is a somewhat unexpected use, but is not
uncommon.)
Using Interfaces for Symbolic
Constants
• In addition to containing method declarations,
interfaces can contain constants, that is,
variables that are public final static.
interface OlympicMedal {
static final String GOLD = "Gold";
static final String SILVER = "Silver";
static final String BRONZE = "Bronze";
}
m1() Cn
m2() m1() // concrete
m3() m2() // concrete
m3() // concrete
C1 C2
m1() // concrete m1() // concrete
m2() // concrete m2() // concrete
m3() // concrete m3() // concrete
Default Method in Java Interfaces
I1
<<interface>>
m1() Cn
m2() m1() // concrete
m3() m2() // concrete
m4() m3() // concrete
m4() // concrete
C1 C2
m1() // concrete m1() // concrete
m2() // concrete m2() // concrete
m3() // concrete m3() // concrete
m4() // concrete m4() // concrete
Default Method in Java Interfaces
I1
<<interface>>
m1() Cn
m2() m1() // concrete
m3() m2() // concrete
default m4()//concrete m3() // concrete
C1 C2
m1() // concrete m1() // concrete
m2() // concrete m2() // concrete
m3() // concrete m3() // concrete
Default Method in Java Interfaces
• Java 8 introduces “Default Method”, a new feature
– Add new methods to the interfaces without
breaking the existing implementation of these
interface.
public interface A {
public void m1();
default public void m2 {
...
println("default m2");
B b = new B();
}
b.m2(); // print "default m2"
}
...
public class B implements A
{
public void m1() {…}
}
Why default keyword is introduced
in interfaces?
• Suppose we need to add a new method in an
existing interface.
• Obviously the old code will not work as the
classes have not implemented those new
methods.
• So with the help of default implementation,
we will give a default body for the newly added
functions. Then the old codes will still work.
Why default keyword is introduced in interfaces?
public interface I1 {
void m1(); Example 1
}
public interface I2 {
void m2();
}
Source: Source: Udemy Master Object Oriented Design in Java - Homework + Solutions
Q & A?
Q: A local variable is not accessible outside the
block it is declared. Therefore, no access
modifier is required for a local variable?
A: TRUE
Q & A?
Q: An instance variable exists as long as the
associated object exists.
A local variable is created every time declaration
statement is executed during the method
execution and the variable ceases to exist upon
the completion of the block it is declared.
A: TRUE
Static methods vs Instance methods in Java
Static methods vs Instance methods in Java
• Instance method are methods which require an
object of its class to be created before it can be
called.
• To invoke a instance method, we have to create
an Object of the class in within which it defined.
Source: https://www.geeksforgeeks.org/static-methods-vs-instance-methods-java/
Static methods vs Instance methods in Java
• Memory allocation: These methods themselves
are stored in Permanent Generation space of
heap but the parameters (arguments passed to them) and
their local variables and the value to be returned
are allocated in stack.
– They can be called
• within the same class in which they reside OR
• from the different classes defined either in the same
package or other packages
depend on the access type provided to the desired
instance method.
Source: https://www.geeksforgeeks.org/static-methods-vs-instance-methods-java/
Static methods vs Instance methods in Java
• Important Points:
• Instance method(s) belong to the Object of the
class not to the class
– i.e. they can be called after creating the Object of the
class.
• Every individual Object created from the class
has its own copy of the instance method(s) of
that class.
• They can be overridden since they are resolved
using dynamic binding at run time.
Source: https://www.geeksforgeeks.org/static-methods-vs-instance-methods-java/
Static methods vs Instance methods in Java
import java.io.*;
class Foo{
String name = "";
// Instance method to be called within the same class or
// from a another class defined in the same package or in different package.
public void myInstanceMethod(String name){
this.name = name;
}
}
class TestTest{
public static void main (String[] args) {
// create an instance of the class.
Foo ob = new Foo();
// calling an instance method in the class 'Foo'.
ob.myInstanceMethod("222");
System.out.println(ob.name); Output :
} 222
}
Source: https://www.geeksforgeeks.org/static-methods-vs-instance-methods-java/
Static methods vs Instance methods in Java
• Static methods are the methods in Java that can
be called without creating an object of class.
• They are referenced by the class name itself or
reference to the Object of that class.
Source: https://www.geeksforgeeks.org/static-methods-vs-instance-methods-java/
Static methods vs Instance methods in Java
• Memory Allocation: They are stored in
Permanent Generation space of heap as they are
associated to the class in which they reside not to
the objects of that class.
• But their local variables and the passed
argument(s) to them are stored in the stack.
• Since they belong to the class so they can be
called to without creating the object of the class.
Source: https://www.geeksforgeeks.org/static-methods-vs-instance-methods-java/
Static methods vs Instance methods in Java
• Important Points:
• Static method(s) are associated to the class in which
they reside i.e. they can be called even without
creating an instance of the class i.e
ClassName.methodName(args).
• They are designed with aim to be shared among all
Objects created from the same class.
• Static methods can not be overridden. But can be
overloaded since they are resolved using static
binding by compiler at compile time.
Source: https://www.geeksforgeeks.org/static-methods-vs-instance-methods-java/
Static methods vs Instance methods in Java
import java.io.*;
class OOAD{
public static String myName = "";
public static void myStaticMethod(String name){ Output :
myName = name; vaibhav
}
} mohit
class TestTest{
public static void main (String[] args) {
// Accessing the static method myStaticMethod () and field by class name itself.
OOAD. myStaticMethod("vaibhav");
System.out.println(OOAD.myName);
// Accessing the static method myStaticMethod () by using Object's reference.
OOAD.obj = new OOAD();
obj. myStaticMethod("mohit");
System.out.println(obj.myName);
}
}
Source: https://www.geeksforgeeks.org/static-methods-vs-instance-methods-java/
Static variables and their values
• Static variables and their values (primitives or
references) defined in the class are stored
in PermGen space of memory.
Source: https://www.geeksforgeeks.org/static-methods-vs-instance-methods-java/
final keyword in Java
• final keyword is used in different contexts.
• First of all, final is a non-access
modifier applicable only to a attribute, a method
or a class.
• Following are different contexts where final is
used.
– final Attributes To create constant attributes
– final Methods To prevent Method overriding
– final Classes To prevent Inheritance
Source: https://www.geeksforgeeks.org/final-keyword-java/
final keyword in Java
• final keyword is used in different contexts.
• First of all, final is a non-access
modifier applicable only to a attribute, a method
or a class.
• Following are different contexts where final is
used.
– final Attributes To create constant attributes
– final Methods To prevent Method overriding
– final Classes To prevent Inheritance
Source: https://www.geeksforgeeks.org/final-keyword-java/
final attribute in Java
• When a attribute is declared with final keyword,
its value can’t be modified, essentially, a constant.
– This also means that you must initialize a final
attribute .
• If the final attribute is a reference, this means
that the variable cannot be re-bound to reference
another object, but internal state of the object
pointed by that reference attribute can be changed
i.e. you can add or remove elements from final
array or final collection.
• It is good practice to represent final attribute in ALL
UPPERCASE, using underscore to separate words.
Source: https://www.geeksforgeeks.org/final-keyword-java/
final attribute in Java
Error:
The blank final field i may not have been
initialized
Source: https://www.geeksforgeeks.org/final-keyword-java/
final attribute in Java
Output:
222 0
Source: https://www.geeksforgeeks.org/final-keyword-java/
final attribute in Java
public class TestFinalAttribute1 {
Source: https://www.geeksforgeeks.org/final-keyword-java/
final attribute in Java
public class TestFinalAttribute2 {
public static void main(String args[]) {
final OtherClass obj = new OtherClass();
obj.i = 30; // Works
}
}
class OtherClass {
int i = 10;
}
Source: https://www.geeksforgeeks.org/final-keyword-java/
final method in Java
• final keyword is used in different contexts.
• First of all, final is a non-access
modifier applicable only to a attribute, a method
or a class.
• Following are different contexts where final is
used.
– final Attributes To create constant attributes
– final Methods To prevent Method overriding
– final Classes To prevent Inheritance
Source: https://www.geeksforgeeks.org/final-keyword-java/
public class TestFinalMethod extends Demo {
Source: https://www.geeksforgeeks.org/final-keyword-java/
final class in Java
Source: https://www.geeksforgeeks.org/final-keyword-java/
static vs. final
Basis For static final
Comparison
Applicable keyword is applicable to nested static class, keyword is applicable to class,
variables, methods and block. methods and variables.
Initialization NOT compulsory to initialize the static compulsory to initialize the
variable at the time of its declaration. final variable at the time of
its declaration.
Modification The static variable can be reinitialized. The final variable CAN NOT
be reinitialized.
Methods static methods can only access the final methods can not be
static members of the class, and can only overridden.
be called by other static methods.
Class static methods class’s object can not be final class can not be
created, and it only contains static inherited by any class.
members only.
Block static block is used to initialize the final keyword supports NO
static variables. such block.
static block in Java
class Test {
static int i;
int j;
static {
i = 10;
System.out.println("static block called ");
}
Test(){
System.out.println("Constructor called");
}
}
class Main {
public static void main(String args[]) {