The Artima Developer Community
Sponsored Link

Java Buzz Forum
How to call non static method from static method 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.
How to call non static method from static method java Posted: Sep 24, 2016 7:10 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: How to call non static method from static method 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
  • Class is a template and when we create object instance variables gets memory.
  • If we create two objects variables get memory in two objects. So instance variables gets memory whenever object is created.
  • When we create variables as static , memory will not be created in objects because static means class level and it belongs to class not object but we can access static variables and static methods from objects.
  • Calling static method from non static method in java 
  • In our scenario calling a non static method from static method in java.
  • If we are calling a non static method then we need to use object so that it will call corresponding object non static method.
  • Non static methods will be executed or called by using object so whenever we want to call a non static method from static method we need to create an instance and call that method.
  • If we are calling non static method directly from a static method without creating object then compiler throws an error. 



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


calling non static method from static method.png

  • In the above program we are trying to call non static method of class from a static method so compiler throwing error.
  • Can not make a static reference to the non- static method nonStaticMethod() from the type StaticMethodDemo

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

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

  1. non static method

Read: How to call non static method from static method java

Topic: Best Deals of the Week, September 19th - September 23rd - Deal Alert Previous Topic   Next Topic Topic: Java Tutorial : Java IO (Java Serialization_V5)

Sponsored Links



Google
  Web Artima.com   

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