Limitations of static methods & static block:-
o Only static data members
of a class can be referred in a static block or method.
o A static block or
static method can directly invoke only static methods.
o ‘this’ and ‘super’
keyword cannot be used in a static method or in
a static block.
Program below executes without ‘main’ method
–
class
Test
{
static
{
System.out.println(“It is executing without main…..”);
System.exit(0);
}
}
Not only we can do this much but
also we can do list of things. See the program below : -
- class Test2
- {
- int a;
- public Test2(int x)
- {
- a = x;
- }
- public void display1()
- {
- System.out.println("a = " + a);
- }
- static
- {
- System.out.println("Test is loaded.");
- Test2 t = new Test2(5);
- t.display1();
- P x = new P();
- System.exit(0);
- }
- }
- class P
- {
- public P()
- {
- System.out.println("P is instantiated.");
- }
- static
- {
- System.out.println("P is loaded.");
- }
- }
OUTPUT -
Now, as we saw that we can do
almost everything without having main( ),
then why we need main( ) in our program?
In the
sessions ahead, we will get the answer.
Passing arguments to methods-
In Java,
primitive type arguments are passed to methods by value i.e. their copy is
created in the invoked method.
Objects
are passed by references i.e. in case of objects, copy of their reference
variables is created in the invoked method.
Now the
program below is written to swap the values.
Observe it carefully
to understand how this issue is being resolved.
- public class Swapper
- {
- public static void swap(int x, int y)
- {
- int z;
- z = x;
- x = y;
- y = z;
- }
- }
- public class SwapperTest
- {
- public static void main(String[ ] args)
- {
- int a =5, b = 6;
- System.out.println("a = " + a);
- System.out.println("b = " + b);
- Swapper.swap(a, b);
- System.out.println("After swap");
- System.out.println("a = " + a);
- System.out.println("b = " + b);
- }
- }
The output is -
Our
purpose is not solved. Let’s see the reason for this.
See the
fig2. below
Now,
let’s find out solution to this problem.
- public class MyNumber
- {
- int value;
- public MyNumber(int x)
- {
- value = x;
- }
- }
- public class Swapper
- {
- public static void swap(MyNumber x, MyNumber y)
- {
- int z;
- z = x.value;
- x.value = y.value;
- y.value = z;
- }
- }
- public class SwapperTest
- {
- public static void main(String[ ] args)
- {
- MyNumber a = new MyNumber(5);
- MyNumber b = new MyNumber(6);
- System.out.println("a = " + a.value);
- System.out.println("b = " + b.value);
- Swapper.swap(a, b);
- System.out.println("After swap");
- System.out.println("a = " + a.value);
- System.out.println("b = " + b.value);
- }
- }
Now the problem is fixed as you can see the value is swapped. We handled it by passing value by reference.
Let see the explanation diagrammatically. (Fig. - 3)
Question. Define a class named
Rational that contains 2 data members
to store the value of numerator and denominator of a rational number, default & two parameterized constructors, a display ( ) method that displays the value of a rational object
in
form and add
( ) methods which are referenced by the following class.
Answer –
- package StaticConceptualPrograms;
- public class Rational{int numerator, denominator;public Rational() { }public Rational(int x, int y){numerator = x;denominator = y;}void display(){System.out.println(numerator+"/"+denominator);}Rational add(Rational y){numerator = y.denominator*this.numerator + this.denominator * y.numerator;denominator = y.denominator*this.denominator;return this;}Rational add(Rational x, Rational y){numerator = y.denominator*x.numerator + x.denominator * y.numerator;denominator = y.denominator*x.denominator;return this;}/*static Rational add(Rational x, Rational y){}*/}package StaticConceptualPrograms;public class RationalTest{public static void main(String[] args){Rational a = new Rational(2, 3);Rational b = new Rational(4, 5);System.out.print("Rational a is : ");a.display();System.out.println("");System.out.print("Rational b is : ");b.display();System.out.println("");Rational c = a.add(b);
- System.out.print("Sum of a & b : ");c.display();System.out.println("");Rational r = new Rational(6, 5);Rational s = new Rational(4, 3);Rational t = new Rational();t.add(r, s);System.out.print("Rational r is : ");r.display();System.out.println("");System.out.print("Rational s is : ");s.display();System.out.println("");System.out.print("Rational r & s is : ");t.display();System.out.println("");Rational z;System.out.print("Sum of a, b, r & s is : ");//z = Rational.add(c, t);//z.display();}
- }
Output -
- public class Rational{int p, q;public Rational() {}
- public Rational(int x, int y){p = x;q = y;}public void display(){System.out.println(p+"/"+q);}Rational add(Rational r){Rational s = new Rational();s.p = p * r.q + q * r.p;s.q = q * r.q;return s;}public void add(Rational a, Rational b){p = a.p * b.q + a.q * b.q;q = a.q * b.q;}public static Rational add(Rational a, Rational b){Rational c = new Rational();c.p = a.p * b.q + a.q * b.q;c.q = a.q * b.q;return c;}}public class RationalTest{public static void main(String[] args){Rational a = new Rational(2, 3);Rational b = new Rational(4, 5);System.out.print("Rational a is : ");a.display();System.out.println("");System.out.print("Rational b is : ");b.display();System.out.println("");Rational c = a.add(b);System.out.print("Sum of a & b : ");c.display();System.out.println("");Rational r = new Rational(6, 5);Rational s = new Rational(4, 3);Rational t = new Rational();t.add(r, s);
- System.out.print("Rational r is : ");r.display();System.out.println("");System.out.print("Rational s is : ");s.display();System.out.println("");System.out.print("Rational r & s is : ");t.display();System.out.println("");Rational z;System.out.print("Sum of a, b, r & s is : ");z = Rational.add(c, t);z.display();}}
To understand the logic used inside the different methods of Rational class, please see the diagrams below
Figure
(20.a) to understand public Rational
add(Rational r) method
Program -
- class Test1{int a, b;public Test1(int a, int b){a=a;b=b;}public static void main(String[] args){Test1 t = new Test1(5, 6);t.display();}public void display(){System.out.println("a = " + a);System.out.println("b = " + b);
- }}
Output -
thanks for alll.......
ReplyDeleteOracle Training in Chennai
Impressive. Your story always bring hope and new energy. Keep up the good work.
ReplyDeleteData Science Training in Indira nagar
Data Science Training in btm layout
Python Training in Kalyan nagar
Data Science training in Indira nagar
Data Science Training in Marathahalli | Data Science training in Bangalore
Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
ReplyDeletepython course institute in bangalore
python Course in bangalore
python training institute in bangalore
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeleteOnline DevOps Certification Course - Gangboard
Best Devops Training institute in Chennai
ReplyDeleteWhen I initially commented, I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several emails with the same comment. Is there any way you can remove people from that service? Thanks.
AWS Training in Bangalore | Best Amazon Web Services Training in Bangalore
Advanced Amazon Web Services Training in Pune | Best AWS Training in Pune
AWS Online Training | Best Online AWS Certification Course - Gangboard
Best Top 110 plus AWS Interview Question and Answers
ReplyDeleteangularjs-Training in pune
angularjs Training in bangalore
angularjs Training in bangalore
angularjs Training in chennai
automation anywhere online Training
angularjs interview questions and answers
The article is so informative. This is more helpful. Thanks for sharing.
ReplyDeleteBest online software testing training course institute in chennai with placement
Best selenium testing online course training in chennai
Learn best software testing online certification course class in chennai with placement
very good article it's very useful web design company in velachery
ReplyDeletevery interesting, good job and thanks for sharing such a good blog. Thanks a lot…
ReplyDeleteBangalore Training Academy is a Best Institute of Salesforce Admin Training in Bangalore . We Offer Quality Salesforce Admin with 100% Placement Assistance on affordable training course fees in Bangalore. We also provide advanced classroom and lab facility.
Enjoyed reading the article above, really explains everything in detail,the article is very interesting and effective.Thank you and good luck…
ReplyDeleteAdvance your career as a SharePoint Admin Engineer by doing SharePoint Admin Courses from Softgen Infotech located @BTM Layout Bangalore.
Such a great word which you use in your article and article is amazing knowledge. Thank you for sharing it.
ReplyDeleteBecame An Expert In UiPath Course ! Learn from experienced Trainers and get the knowledge to crack a coding interview, @Softgen Infotech Located in BTM.
I'd love to thank you for the efforts you've made in composing this post. I hope the same best work out of you later on too. I wished to thank you with this particular sites! Thank you for sharing. Fantastic sites!
ReplyDeleteangular js training in chennai
angular js training in tambaram
full stack training in chennai
full stack training in tambaram
php training in chennai
php training in tambaram
photoshop training in chennai
photoshop training in tambaram
Great Article
ReplyDeleteIoT Projects for Students
Deep Learning Projects for Final Year
JavaScript Training in Chennai
JavaScript Training in Chennai
The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training
====================================================================================================================================
Good blog, it's really very informative, do more blogs under good concepts.
ReplyDeleteartificial intelligence and machine learning
how to learn react js
services provided by amazon
angularjs web application
aws interview questions and answers for experienced
devops interview questions and answers for experienced
Liên hệ Aivivu, đặt vé máy bay tham khảo
ReplyDeletevé máy bay đi Mỹ giá bao nhiêu
đặt vé máy bay từ mỹ về việt nam
các đường bay từ canada về việt nam
giá vé nhật việt
gia ve may bay tu han quoc ve viet nam
Vé máy bay từ Đài Loan về VN
khách sạn cách ly ở việt nam