The Artima Developer Community
Sponsored Link

Java Buzz Forum
Calling static method from non static method in java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Calling static method from non static method in java Posted: Sep 24, 2016 4:59 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Calling static method from non static method in java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • Static means class level and non static means object level.
  • Non static variable gets memory in each in every object dynamically.
  • Static variables are not part of object and while class loading itself all static variables gets memory.
  • Like static variables we have static methods. Without creating object we can access static methods.
  • Static methods are class level. and We can still access static methods in side non static methods.
  • We can call static methods without using object also by using class name.
  • And the answer to the question of  "is it possible to call static methods from non static methods in java" is yes.
  • If we are calling a static method from non static methods means calling a single common method using unique object of class which is possible. 


Program #1: Java example program to call static method from non static method.


  1. package com.instanceofjava.staticinterviewquestions;
  2. public class StaticMethodDemo {
  3.  
  4. void nonStaticMethod(){
  5.         
  6.         System.out.println("Hi i am non static method");
  7.         staticMethod();
  8.  }
  9.     
  10.  public static void staticMethod(){
  11.         
  12.         System.out.println("Hi i am static method");
  13.   }
  14.     
  15.  public static void main(String[] args) {
  16.         StaticMethodDemo obj= new StaticMethodDemo();
  17.         
  18.         obj.nonStaticMethod();
  19.  
  20.     }
  21.  
  22. }
 Output:

  1. Hi i am non static method
  2. Hi i am static method

  • In the above program we have created object of the class and called a non static method on that object and in side non static method called a static method.
  • So it is always possible to access static variables and static methods in side non static methods

 Program #2: Java example program to call static method from non static method.


Read: Calling static method from non static method in java

Topic: Java Tutorial : Java IO (Java Serialization_V5) Previous Topic   Next Topic Topic:

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use