Java4teachus

Tuesday, May 28, 2019

Instance Block In Java

Instance Static Block In Java


The Purpose of instance Static block is to initialize the instance "Data Members" of a class .In other words The Functionality of instance block is similar to the functionality of default constructor of a class .

The execution behavior of instance block is that "instance block called will be called Java virtual mechanic (JVM) each every time whenever object is created and it is executed by Jvm before executing constructor and other executing static  block ".
Instance-Static-Block-In-Java.jpeg

Syntax :-

{
Block of statement (s) initialization 
}
Instance block will be written always within the Class definition .

Q. Write a Java Program which illustrate initializing the data instance data member by instance block .

Program:-

Class Student 
{
int stno;
String name;
static 
{
System.out.println("static block called");
}
{
System.out.println("Instance block called ");
stno=10;
name="SATYA";
System.out.println("val of stno="+ stno) ;
System.out.println("val of stname="+ name);
}
Student()
{
System.out.println("constructor called");
}
}
Class StudentInfo 
{
public static void main(String args[])
{
Student s1 = new Student();
Student s2 = new Student();
}
}
     

Sunday, May 19, 2019

Static Block in Java

Introduction Of Static Block


we know that as a part of class, we can define the instance data members & static data members in java program. To initialize this data members we used already in the constructor concept.

If we initialize instance data members and static data members in constructors then we get advantages of initialize instance data members but not recommended to initialize data members, because static data members values are replacing the same multiple times, which will take more execution time. To overcome this problem we use a concept called static block


Purpose Of Static Block


The Purpose of static block is used to the static data members of a class only once.

Static block will execute only once when the class is loaded the main memory.

Syntax-Of-Static-Block.jpeg


Static block will not return only value that is not return type. Static block is always written in the class definition but not the method definition.

Constructor will execute each and every time for initializing the instance data member but the static block will execute the only one time when the class is loaded by the static data members.

If we write the main method on static block in single block in single java program first static block will be call later main() method calls only once.+

Writing a static block is optional, whenever we initialize the static block  that time of point we write the static block.

Q . Write a java program which will initialize the data members with constructor by using static data member in the static block.

Program:-

class Student
{
int Id;
String name;
float marks;
static String crs;
static
{
crs="Java";
}
Student(int studentid, String studentname, float studentmarks)
{
Id=studentid;
name=studentname;
marks=studentmarks;
System.out.println("Student Id="+studentid);
System.out.println("Student name="+studentname);
System.out.println("Student marks="+studentmarks);
System.out.println("Student course="+crs);
}
}
class StudentInfo1
{
public static void main(String[] args)
{
Student s=new Student(1,"Lucky",80.1f);
Student s1=new Student(2,"Jameg Gossling",90.1f);
Student s2=new Student(3,"KVR",75.1f);
}
}

Saturday, May 11, 2019

Write a java program by Using Scanner Class

Reading the values from keyboard In java By Scanner Class


Q. Write a Java program which will read the 2 integer values Dynamically and add them.

Program:-


import java.util.Scanner;

class  Sum
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the First Value");
int x1=s.nextInt();
System.out.println("Enter the Second Value");
int x2=s.nextInt();
int x3=x1+x2;
System.out.println("sum="+x3);
}
}

Output:-

D:\java>javac Sum.java ↵

D:\java>Java Sum 
Enter the First value
12
Enter the Second value
12
Sum = 24

Scanner-Class-Programs-In-Java.jpeg


Q. Write a Java program which will read the 2 integer values Dynamically and Multiply them.


Program:-


import java.util.Scanner;

class  Multiply
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the First Value");
int x1=s.nextInt();
System.out.println("Enter the Second Value");
int x2=s.nextInt();
int x3=x1*x2;
System.out.println("Multiply="+x3);
}
}

Output:-

D:\java>javac  Mutliply.java ↵

D:\java>Java Mutliply
Enter the First value
2
Enter the Second value
2
Multiply = 4

Q. Write a Java program which will read the 2 String values Dynamically and concat them.


import java.util.Scanner;
class Names
{
public static void main(String[] args) 
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the first name");
String a=s.nextLine();
System.out.println("Enter the Second name");
String b=s.nextLine();
String c=a+b;
System.out.println("Concat="+c);
}
}

Output:-

D:\java>javac Names .java ↵
D:\java>Java Names
Enter the First Name
Lucky
Enter the Second Name
Bujji
Concat=LuckyBujji

Q. Write a java program to display the student details with dynamically .


Program:-


import java.util.Scanner;

class Student
{
int studentid;
String name;
float marks;
String classname;
void studentInfo()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the Student ID");
studentid=Integer.parseInt(s.nextLine());
System.out.println("Enter the Student name");
name=s.nextLine();
System.out.println("Enter the Student Marks");
marks=Float.parseFloat(s.nextLine());
System.out.println("Enter the Student classname");
classname=s.nextLine();
}
void displayStudentInfo()
{
System.out.println("Enter the Student ID="+studentid);
System.out.println("Enter the Student Name="+name);
System.out.println("Enter the Student marks="+marks);
System.out.println("Enter the Student class="+classname);
}
}
class  StudentInfo
{
public static void main(String[] args) 
{
Student s1=new Student();
s1.studentInfo();
s1.displayStudentInfo();
}
}

Saturday, May 04, 2019

What is Scanner Class in Java

Reading The  Data From Keyboard


To Read data from the keyboard in java, we can use pre-defined  class called "Scanner" and it is present in package called   "java.util.*" 

When we use Scanner class its a part of Java Program we must import "Java .Util.*"; Other Wise we get compile time error will occurs in java.

Scanner-Class-In-Java.jpeg

Scanner is one of the new facility present in JDK 1.5 version onward Reading the data form the keyboard is nothing but creating an object of Scanner class.

Profile of java .Util .*:-

1) Constructor
    a)Scanner (InputStream)
2) Instance Methods
    a)public String nextLine( )
    b)public String nextLine( )

➤ Constructor 1 is used for creating an object of Scanner class. By passing an object of  "InputStream".
 An object of Input Stream called "in" created as a static data member in an another predefined class called System & hence it must be access as  " System.in ".

EX:- Create an object of Scanner  

Scanner S = new scanner (System.in);

here S is an object of Scanner Class and it use  to the read data from the keyboard.
here System.in it provides an environment to read the data form.

Method 1 : Represents any fundamental data type
Method 2 : Used for reading any fundamental data type from keyboard
Method 3 : Used for reading any type of data in the string type.