Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

core java concepts

Static variables and static methods

Particular method common to all instances of class Also called as class level variables Static method can access only static data

Static variables and static methods

Syntax
Static Static

datatype variable name; returntype methodname(parameters); int a; int mul(int a, int b)

Static

Static

{ }

How to call static methods

We can directly the method using class name Classname.methodname; Sample.mul();

Objective of static member or method


Member common to all instance of class Class level variables Only one copy of the member It avoid multiple instance to call the method Static method are accessed directly using class name
Java.math.*

methods)

Package(contains 80 static

Math.sqrt(a,b)

Class staticdemo { Static int a; Static void display() { Return a*a; } Psvm { int a; a=staticdemo.display(); System.out.println(a); } }

Packages

Collection of interface of classes Namespace for organizing classes and interface in a logical manner Each programmer can define their own namespace and place their code within that namespace, thus two classes that have exact same name are now distinguishable since they occur in different namespace.

Built-in and user defined Packages

Java Application Programming Interface Packages(API):


Java.lang Java.util Java.io Java.awt Java.applet

Java User Defined Packages:


Packages

created by user and it can be imported in any java program

Java package hierarchy

Creation of user defined Package


Package packagename; Package app.empdetails; Public class Employee { }

Importing user defined package


Import app.empdetails.Employee; Public class Director extends Employee { }

Exception Handling

Exception Handling

Error
Compile

time error //syntax error

Logical

error // mistaken in loop error //only 3 element s present in the

Runtime

array but we are trying to execute 4th element

Exception Handling

Error occurs during runtime of the program called exception Checked Exception: Program handles the exception Unchecked Exception: automatically handled by JVM

Exception Handling

Types of Exception
Runtime

Exception Exception

Input/Output

Exception Handling

Input/Output Exception:

File not found exception Stack overflow Class not found exception Number format exception

Runtime Exception

ArithmeticException ArrayoutofBoundException StringIndexoutofBoundException NullpointerException ArraystoreException OutofMemoryException SecurityException

Exception Handling
try { } Catch { } Finally { }

Exception Handling

Messages:
getmessage(); getstacktrace(); PrintStacktrace();

Exception Handling
Class excepdemo { Psvm { int a=15; int b=5,c=5; Try { int result = a/ b-c; } Catch(ArithmeticException e) { System.out.println(Message+e.getmessage()); } } }

Thank you

You might also like