Skip to main content

Posts

Showing posts from October, 2015

System.out.println In Java

System  is a class in the  java .lang package.  out  is a static member of the  System  class, and is an instance of  java .io.PrintStream .  println  is a method of  java .io.PrintStream . This method is overloaded to print message to output destination, which is typically a console or file. System is a class, that has a public static field out . So it's more like. public final class System { /** * The "standard" output stream. This stream is already * open and ready to accept output data. Typically this stream * corresponds to display output or another output destination * specified by the host environment or user. * <p> * For simple stand-alone Java applications, a typical way to write * a line of output data is: * <blockquote><pre> * System.out.println(data) * </pre></blockquote> * <p> * See the <code>println...

Object serialization in java

Serialization is the conversion of an object to a series of bytes, so that the object can be easily saved to persistent storage or streamed across a communication link. The byte stream can then be deserialized - converted into a replica of the original object. When you want to serialize an object, that respective class should implement the marker interface serializable. It just informs the compiler that this java class can be serialized. You can tag properties that should not be serialized as transient. You open a stream and write the object into it. Code for serialization of a java class : Data.java package com . codetalk . serialization ; import java.io.Serializable ; public class Data implements Serializable { private static final long serialVersionUID = 1L; private String firstName ; private String lastName ; /** * @return the firstName */ public String getFirstName () { return firstName ; } /** * @param firstNam...

Why is the Java main method static ?

The method signature of a Java  main()  method is: public static void main ( String [] args ){ ... } Before the main method is called, no objects are instantiated. Having the static keyword means the method can be called without creating any objects first. 1. Since main method is static JVM can call it without creating any instance of class which contains main method.  2.  If main method were not declared static than JVM has to create instance of main Class and since constructor can be overloaded and can have arguments there would not be any certain and consistent way for JVM to find main method in Java. Let's simply pretend, that  static  would not be required as the application entry point. A application class would then look like this: class MyApplication { public MyApplication (){ // Some init code here } public void main ( String [] args ){ // real application code here ...