Class 7 Con Static
Class 7 Con Static
Java Constructors
A constructor in Java is similar to a method that is invoked when an object of
the class is created.
Unlike Java methods, a constructor has the same name as that of the class
and does not have any return type. For example,
class Test {
Test() {
// constructor body
}
}
Here, Test() is a constructor. It has the same name as that of the class and
doesn't have a return type.
// constructor
Main() {
System.out.println("Constructor Called:");
name = "Programiz";
}
Output:
Constructor Called:
The name is Programiz
Inside the constructor, we are initializing the value of the name variable.
Notice the statement creating an object of the Main class.
Here, when the object is created, the Main() constructor is called. And the
value of the name variable is initialized.
Hence, the program prints the value of the name variables as Programiz .
Types of Constructor
In Java, constructors can be divided into three types:
1. No-Arg Constructor
2. Parameterized Constructor
3. Default Constructor
Class-7-Constructor-Static
private Constructor() {
// body of the constructor
}
int i;
Output:
Constructor is called
Value of i: 5
Here, the constructor does not accept any parameters. Hence, it is known as
a no-arg constructor.
// public constructor
public Company() {
name = "Programiz";
}
}
class Main {
public static void main(String[] args) {
Output
String languages;
Output
Based on the argument passed, the language variable is initialized inside the
constructor.
int a;
boolean b;
System.out.println("Default Value:");
System.out.println("a = " + obj.a);
System.out.println("b = " + obj.b);
}
}
Run Code
Output
Default Value:
a = 0
b = false
boolean false
byte 0
short 0
int 0
long 0L
Class-7-Constructor-Static
char \u0000
float 0.0f
890
System.out.println("Default Value:");
System.out.println("a = " + obj.a);
System.out.println("b = " + obj.b);
}
}
Run Code
Output
Default Value:
a = 0
b = false
Constructor types:
No-Arg Constructor - a constructor that does not accept any arguments
Parameterized constructor - a constructor that accepts arguments
Default Constructor - a constructor that is automatically created by the Java
compiler if it is not explicitly defined.
A constructor cannot be abstract or static or final .
is used for memory management mainly. We can apply static keyword with variables
. The static keyword belongs to the class than an instance of the class.
o The static variable can be used to refer to the common property of all objects
(which is not unique for each object), for example, the company name of
employees, college name of students, etc.
o The static variable gets memory only once in the class area at the time of
class loading.
2. class Student{
3. int r;//instance variable
4. String n;
5. static String college ="ITS";//static variable
6. //constructor
7. Student(int r, String n){
8. This.r= r;
9. This.n = n;
10. }
11. //method to display the values
12. void display (){
13.System.out.println(rollno+" "+name+" "+college);
14.}
15.}
16.//Test class to show the values of objects
17.public class TestStaticVariable1{
18. public static void main(String args[]){
Student s1 = new Student(111,"Karan");
19. Student s2 = new Student(222,"Aryan");
20. //we can change the college of all objects by the single line of code
21. //Student.college="BBDIT";
22. s1.display();
23. s2.display();
24. }
25.}
Test it Now
Output:
1. //Java Program to get the cube of a given number using the static method
2.
3. class Calculate{
4. static int cube(int x){
5. return x*x*x;
6. }
7.
8. public static void main(String args[]){
9. int result=Calculate.cube(5);
10. System.out.println(result);
11. }
12.}
Test it Now
Output:125
Suggestion: If you are beginner to java, lookup only three usages of this
keyword.
Class-7-Constructor-Static
17.s1.display();
18.s2.display();
19.}}
Test it Now
Output:
Output:
1. class A{
2. void m(){
3. System.out.println("hello m");
4. }
5. void n(){
6. System.out.println("hello n");
7. //m();//same as this.m()
8. this.m();
9. }
10.}
11.class TestThis4{
12.public static void main(String args[]){
13.A a=new A();
14.a.n();
15.}}
Test it Now
Output:
Class-7-Constructor-Static
hello n
hello m
1. class A{
2. A(){
3. System.out.println("hello a");
4. }
5. A(int x){
6. this();
7. System.out.println(x);
8. }
9. }
10.class TestThis5{
11.public static void main(String args[]){
12.A a=new A(10);
13.}}
Test it Now
Output:
hello a
10
1. class A{
2. A(){
3. this(5);
4. System.out.println("hello a");
5. }
6. A(int x){
Class-7-Constructor-Static
7. System.out.println(x);
8. }
9. }
10.class TestThis6{
11.public static void main(String args[]){
12.A a=new A();
13.}}
Test it Now
Output:
ADVERTISEMENT
ADVERTISEMENT
5
hello a
Output:
26.s1.display();
27.s2.display();
28.}}
Test it Now
Output:
ADVERTISEMENT
ADVERTISEMENT
1. class S2{
2. void m(S2 obj){
3. System.out.println("method is invoked");
4. }
5. void p(){
6. m(this);
7. }
8. public static void main(String args[]){
9. S2 s1 = new S2();
10. s1.p();
11. }
12.}
Test it Now
Output:
method is invoked