All Java Code With Example
All Java Code With Example
import java.util.Scanner;
class Outer
{
int x;
int y;
class Add
{
Scanner sc = new Scanner(System.in);
void InnerDisplay()
{
x=sc.nextInt();
y=sc.nextInt();
System.out.println("The sum of two number is : "+(x+y));
}
}
class Minus
{
Scanner sc = new Scanner(System.in);
void InnerDisplay()
{
x=sc.nextInt();
y=sc.nextInt();
void display()
{
Add a = new Add();
a.InnerDisplay();
Minus m = new Minus();
m.InnerDisplay();
}
}
class Main
{
public static void main (String[] args) {
Outer o = new Outer();
o.display();
}
}
here also i learn one more thing that scanner class doesnot work on class if we
don't write it on method block then it don't work.
2.Anonymas Class
interface Test
{
void display();
}
class Outer
{
public void math()
{
Test t = new Test()
{
public void display()
{
System.out.println("Hi There! lol happend to him");
}
};
}
}
class Main
{
public static void main (String[] args) {
Outer o = new Outer();
o.math();
}
}
>>>> if you want to access a static varriable then you just don't need nothing just
need what is class name.method name thats it under sop then it's perfect and ready
to compile
>>>final varriable should be write in capital letter and for inialize we ued
static block and one more way used constructor,and another way is when intialize
write the value,and afeter intialize we don't replace the value and in static
method we don't use this and super method
>>>>>and for final we can not use subclass and final method can not be override and
final class can not be extended
example of constructor with passing the value used liked method
class Test
{
final int X;
final int Y;
public Test(int l,int m)
{
X=l;
Y=m;
System.out.println("here the final two value is : "+X,+Y);
}
}
public class Main
{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int x;
int y;
System.out.println("Please enter two value : ");
x=sc.nextInt();
y=sc.nextInt();
Test t = new Test(x,y);
}
}
>>>>i need ot cover the java package after all the study i have learned (including
basic things like how to create a pakages using cmd and how to access thess live)
>>>>>Acesss Modifire
inner class can be private default anything outer class can not private and
protected
only public and default
test (has a) object of demo one and for inheriting (is a) such case like demo
extends demo-one
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("rest of the code");
}
}
...here multiple inner catch and try block and one outer try block and one outer
catch block
import java.util.Scanner;
arr[2]=value;
//second try block
try{
int ot =arr[2]/0;
}
//second cathc block
catch(ArithmeticException e)
{
System.out.println("Sorry any number is not divisible by zero asked
your developer to changed the demonitor to zero to any number");
}
}
catch(Exception e)
{
System.out.println("time invesxt for your brain for your life don't use
the time for another thing which don't give you happiness ");
}
}
}
>>>>another class method throws exception and handle by main class and also solve
the exception by catch
import java.util.Scanner;
class test
{
public void divisor(int monitor,int demonitor)throws ArithmeticException
{
int result;
result=monitor/demonitor;
System.out.println("Here rsult is : "+result);
}
}
class Main
{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int up;
int down;
System.out.println("please enter monitor : ");
up=sc.nextInt();
System.out.println("please enter demonitor : ");
down=sc.nextInt();
test t = new test();
try {
t.divisor(up,down);
} catch(ArithmeticException e) {
System.out.println("you should not enter demonitor value zero thats why
we add two in your demonitor either you showed up a infiniy "+up/(down+2));
}
}
}
>>>>>we cannot use overriden method from super class to subclass for IOException
and though i test this code for arithmeticexception there override method work
succesfully.
import java.io.*;
class Parent{
>>>>>>>>second one
import java.io.*;
class Parent{
>>>>>here you see where you use same method but that's not a big issue you use same
method for parent class and child class and in two method you create exception but
child class run
import java.io.*;
class Parent{
void msg() throws Exception {
System.out.println("parent method");
}
}
try {
p.msg();
}
catch(Exception e) {}
}
}
>>>create custom exception class and use it for exception
import java.util.Scanner;
class Main
{
public static void liggle(int age)throws test
{
if(age<18)
{
throw new test("not eligable");
}
else
{
}
}
}
}