0% found this document useful (0 votes)
39 views7 pages

All Java Code With Example

Uploaded by

soccho roy
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
39 views7 pages

All Java Code With Example

Uploaded by

soccho roy
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 7

>>>>Inner class 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();

System.out.println("The minus value is : "+(+x-y));


}
}

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

>>>>>>>>>>>static nested class example

public class TestOuter2{


static int data=30;
static class Inner{
static void msg(){System.out.println("data is "+data);}
}
public static void main(String args[]){
TestOuter2.Inner.msg();//no need to create the instance of static nested class
}
}

>>>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

>>>>>Multiple Exception Handeling

public class Main {

public static void main(String[] args) {

int a[]=new int[5];


try{
System.out.println(a[5]);
}
catch(Exception e)
{
System.out.println(e);
}
try
{
a[5]=30/0;

}
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;

public class Main


{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("please enter how many number : ");
int m;
m=sc.nextInt();

int[ ] arr = new int[m];


try
{
int value;
System.out.println("Please enter which value you want to see : ");
value=sc.nextInt();
//first try block
try
{
System.out.println("here the value of your given index is :
"+arr[value]);
}
//frist cathc block
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("your index value is so high input valid
number");
}
//intialize the value again zero to store value when it's not actually
necessary
value=0;
System.out.println("please enter a value for index : ");
value=sc.nextInt();

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{

// defining the method


void msg() {
System.out.println("parent method");
}
}

public class TestExceptionChild extends Parent{

// overriding the method in child class


// gives compile time error
void msg() throws IOException {
System.out.println("TestExceptionChild");
}
public static void main(String args[]) {
Parent p = new TestExceptionChild();
p.msg();
}
}

>>>>>>>>second one
import java.io.*;
class Parent{

// defining the method


void msg() {
System.out.println("parent method");
}
}

public class TestExceptionChild extends Parent{

// overriding the method in child class


// gives compile time error
void msg() throws ArithmeticException {
System.out.println("TestExceptionChild");
}

public static void main(String args[]) {


Parent p = new TestExceptionChild();
p.msg();
}
}

>>>>>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");
}
}

public class TestExceptionChild3 extends Parent {


void msg()throws Exception {
System.out.println("child method");
}

public static void main(String args[]){


Parent p = new TestExceptionChild3();

try {
p.msg();
}
catch(Exception e) {}
}
}
>>>create custom exception class and use it for exception

import java.util.Scanner;

class test extends Exception


{
public test(String str)
{
super(str);
}

class Main
{
public static void liggle(int age)throws test
{
if(age<18)
{
throw new test("not eligable");
}
else
{

}
}

public static void main (String[] args) {


Scanner sc = new Scanner(System.in);
int age_user;
System.out.println("please enter your age : ");
;
try
{
age_user=sc.nextInt();
liggle(age_user);
}
catch(test t)
{
System.out.println("sorry brother for voting you must be an adult");
System.out.println("Exception type : "+t);
}
System.out.println("Exception handled Successfully");

}
}

You might also like