Documente Academic
Documente Profesional
Documente Cultură
Inner classes
Means defining another class within another class. If class B is within A then B is known in A but not outside A. Enclosed class (B above) has access to all members of enclosing class. (A above) Enclosing class does not have access to members of enclosed class.
class Outer { int outer_x = 100; void test() { Inner inner = new Inner(); inner.display(); }
// this is an innner class class Inner { void display() { System.out.println("display: outer_x = " + outer_x); } } }
class InnerClassDemo { public static void main(String args[]) { Outer outer = new Outer(); outer.test(); } } //Innerclass is known only within the //Outerclass, hence we cannot create an //object of Innerclass in main() method.
class TO { int x; public String toString() { return("Java Java Mahiya Ve.."); } public static void main(String args[]) { TO obj = new TO(); System.out.println(obj); System.out.println(5); System.out.println(new IllegalAccessException()); } }
Converting data
import java.io.*; class Convert { public static void main(String args[]) { int i = 0; double k = 0; try{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); i = Integer.parseInt(br.readLine()); k = Double.parseDouble(br.readLine()); } catch(IOException e) { } System.out.println(i + k); } }
parseInt()
Exists for byte, short, int, long, double but NOT for float. Converts compatible string to appropriate data type. For example: giving 45.678 for parseInt produces error Example: giving Kehva123 produces error. All these throw NumberFormatException