Skip to main content

Posts

Showing posts from December, 2016

Final keyword in Java

The Final keyword is a non-access modifier in java and it can be applied to variable( final variable ) method( final method ) class( final Class ) Final Variable If any variable in java class is declared with the  final keyword is known as the  final variable.   the  final variable value can't be modified once initialized. that means we can access the final variable  value but we cannot modify the final variable value. The final variable value will be constant. Let's see the below code   Example public class FinalDemo { private final int x = 10; public void modify(){ x = 20; System. out .println(x); } public static void main(String[] args) { FinalDemo f = new FinalDemo(); f.modify(); } } output From above we can confirm that we cant modify the final variable value.in simple terms we can say that final variable values are constant values.   Example of accessing the final variable public class Fin...

Generate Random Numbers Program In Java

package com.java2study; import java.util.Random; public class RandomGeneratorDemo { public static void main(String[] args) { Random r = new Random(); for(int i = 1;i int randomNumber = r.nextInt(175); System.out.println("The Random Numer is :"+randomNumber); } } } output The Random Numer is :3 The Random Numer is :32 The Random Numer is :91 The Random Numer is :34 The Random Numer is :135 The Random Numer is :110 The Random Numer is :133

Java Program To Print Ip Address

package com.java2study; import java.net.InetAddress; import java.net.UnknownHostException; public class PrintIpAddress { public static void main(String[] args) { try { System.out.println("your system current ip :"+InetAddress.getLocalHost()); } catch (UnknownHostException e) { e.printStackTrace(); } } } output your system current ip :m-Lenovo-G560/127.0.1.1

Write a Java Program To Print Floyd's Triangle

package com.java2study; import java.util.Scanner; public class FloydsTraingle { /* * 1 * 2 3 * 4 5 6 * 7 8 9 10 */ public static void main(String[] args) { int number = 1; //accept the user input From The KeyBoard Scanner scanner = new Scanner(System.in); System.out.println("Enter The NumberOf Rows:"); int noofrows = scanner.nextInt(); for(int r = 1;r for(int c = 1;c System.out.print(number+" "); number++; } System.out.println(); } } } output Enter The NumberOf Rows: 1 1 ---------------- Enter The NumberOf Rows: 2 1 2 3 ------- Enter The NumberOf Rows: 3 1 2 3 4 5 6 ----- Enter The NumberOf Rows: 4 1 2 3 4 5 6 7 8 9 10

Java Static KeyWord Tutorial

Static is a keyword in java and it can be applied to variables methods blocks nested classes interface static methods (introduced in java8) Static keyword is associated with The Class rather than any Object. Static Variable The variable which can be declared With static keyword is known as static variable. static varibles will keep common value to all objects.Every Object Of The class shares The static variable. Example package com.mypractice; public class Employee { private int employeeId; private String Name; private static String department = "AAAAA" ; public int getEmployeeId() { return employeeId; } public void setEmployeeId(int employeeId) { this .employeeId = employeeId; } public String getName() { return Name; } public void setName(String name) { Name = name; } public static void main(String[] args) { Employee employee1 = new Employee(); employee1.setEmployeeId(1); employee1.setName("A"); System. out .println(em...

Create Immutable Class in Java With Example?

immutable class means if its state cannot change after it is constructed. means once we created object we cant modify exisiting object.here state means instance variable values. In Java String and all Wrapper classes are Immutable. Rules to create Immutable class in java declare the class final ,so that we cannot create subclass. make all fields final and private .so that we cannot change the values. dont write any setter methods in class Example package com.practice; public final class Account { private final int accountId; private final String name; public Account(int accountId, String name) { super(); this.accountId = accountId; this.name = name; } public int getAccountId() { return accountId; } public String getName() { return name; } }

Java Constructor

Java Constructor is a special method  which can be invoked at the time of Object Creation. A constructor is used to initialize the newly created object. For Example, We have a class called Hello  when  you create the object of the Hello Class Hello Class Constructor gets Invoked  like below package com.practice; public class Hello { Hello() { /* Constructor */ System. out .println( "Hello Class Constructor" ); } public static void main(String[] args) { Hello hello = new Hello(); //Object Creation } } output Hello Class Constructor Rules to declare a constructor   A constructor  must have the same name as Class .  A constructor has no return  type explicitly.  A constrcutor can contain all Access Modifiers( private,default,protected,public ).  A constructor cannot contain Non-Access Modifiers( abstract,final,static,synchronized ).  A constructor can have parameters (arguments).  A construc...