Choose Your Language

Wednesday, 3 December 2014

How to access non-static member of a class using it's static method

package com.test.java;

public class Pen {
    String name="Aravind";//non static member
   
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub       
        displayName();
    }

    static void displayName(){
        Pen p=new Pen();
        System.out.println(p.name);
    }
}

Output:
Aravind

No comments:

Post a Comment