Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

all core java concepts:encapsulation,method overloading,method overriding,interfaces, Thesis of Java Programming

all concepts are illustrated with example and difference b/w stactic and non static methods is also explained

Typology: Thesis

2017/2018

Uploaded on 05/14/2018

123dee
123dee 🇮🇳

1 document

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
5/4/2018 Difference Between Static and non-static Method in Java
https://www.sitesbay.com/java/java-static-and-non-static-method 1/3
Basics of Java
JP - History of Java
JP - Overview of Java
JP - Basic Points
JP - Features of Java
JP - Environment Setup
JP - Path vs Classp ath
JP - Platform Independ ent
JP - First java Program
JP - Compile and execute
JP - JDK JVM JRE
JP - JVM Architecture
JP - Object and Class in Java
JP - Datatypes in Java
JP - Variables in Java
JP - Operators in Java
JP - Java Program Structure
JP - Main() Method
JP - CMD Argumen ts
JP - SOP Statements
JP - Decision making statem ents
JP - Looping s tatements
JP - Wrapper Class es
JP - Naming Co nversion
JP - Import Statements in Java
JP - Access Modifiers in Java
JP - Array in Java
Java Useful Keywords
JP - Final Keyword in Java
JP - Static Keyword in Java
JP - This Keyword in Java
JP - Super Keyword in Java
Static and non-Static Method in java
Prev Tut orial Next Tutorial
Difference betwe en Static and non-static me thod in Java
In case of non-static method memory is allocated multiple t ime whenever method is calling. But
memory for static method is allocated only once at th e time of class loading. Method of a class
can be declare d in two different ways
Non-static methods
Static methods
Difference betwe en non-static and static Method
Non-Static meth od Static met hod
1
These method never be preceded by
static keyword
Example:
void fu n1()
{
......
......
}
These method always preced ed by static
keyword
Example:
static v oid fun2()
{
......
......
}
2Memory is allocated multiple time
whenever method is calling.
Memory is allocated only once a t the time of
class loading.
3It is specific to an object so th at these
are also known as instance method.
These are common to every object so that it is
also known as member method or class method.
4
These methods always access with
object refere nce
Syntax:
Objre f.method name();
These prop erty always access with class
reference
Syntax:
className.me thodna me();
5
If any method wants to be execute
multiple time that can be declar e as non
static.
If any method wants to be execute only once in
the progra m that can be d eclare as static .
Note: In so me cases static methods not only can access with class reference but also can
access with object referen ce.
Example of Static and non -Static Me thod
Example
class A
{
void fu n1()
{
System.out.println("Hello I am Non-Static");
}
static v oid fun2()
Download Projects
Advertisements
Download WhatsApp Statu
Video
Buy This Ad Space Only $5
per Month, Ad Size 300X25
Contact on:
hitesh.xc@ gmail.com
WhatsApp no. 8882538826
HOME C C++ DS Java AW T Collection Jdbc JSP Servlet SQL PL/SQL C-Code C++-Code Java-Code Project Interview
Custom Search
S
Ea
Html Html5 CSS JavaScript Ajax JQuery AngularJS JSON GMaps Adsense Blogger Earning Email Domain SEO SMO ABOUT | SERVI CES | FORM S | C
JP - Java Home
-40%
Rs 475
Rs 285
pf3

Partial preview of the text

Download all core java concepts:encapsulation,method overloading,method overriding,interfaces and more Thesis Java Programming in PDF only on Docsity!

Basics of Java

JP - History of Java

JP - Overview of Java

JP - Basic Points

JP - Features of Java

JP - Environment Setup

JP - Path vs Classpath

JP - Platform Independent

JP - First java Program

JP - Compile and execute

JP - JDK JVM JRE

JP - JVM Architecture

JP - Object and Class in Java

JP - Datatypes in Java

JP - Variables in Java

JP - Operators in Java

JP - Java Program Structure

JP - Main() Method

JP - CMD Arguments

JP - SOP Statements

JP - Decision making statements

JP - Looping statements

JP - Wrapper Classes

JP - Naming Conversion

JP - Import Statements in Java

JP - Access Modifiers in Java

JP - Array in Java

Java Useful Keywords

JP - Final Keyword in Java

JP - Static Keyword in Java

JP - This Keyword in Java

JP - Super Keyword in Java

Static and non-Static Method in java

Prev Tutorial Next Tutorial

Difference between Static and non-static method in Java

In case of non-static method memory is allocated multiple time whenever method is calling. But memory for static method is allocated only once at the time of class loading. Method of a class can be declared in two different ways

Non-static methods

Static methods

Difference between non-static and static Method

Non-Static method Static method

These method never be preceded by static keyword Example:

void fun1() { ...... ...... }

These method always preceded by static keyword Example:

static void fun2() { ...... ...... }

2 Memory is allocated multiple timewhenever method is calling. Memory is allocated only once at the time ofclass loading.

3 It is specific to an object so that theseare also known as instance method. These are common to every object so that it isalso known as member method or class method.

These methods always access with object reference Syntax:

Objref.methodname();

These property always access with class reference Syntax:

className.methodname();

If any method wants to be execute multiple time that can be declare as non static.

If any method wants to be execute only once in the program that can be declare as static.

Note: In some cases static methods not only can access with class reference but also can access with object reference.

Example of Static and non-Static Method

Example

class A { void fun1() { System.out.println("Hello I am Non-Static"); } static void fun2()

Download Projects

Advertisements

Download WhatsApp Statu

Video

Buy This Ad Space Only $

per Month, Ad Size 300X

Contact on:

hitesh.xc@gmail.com

WhatsApp no. 8882538826

HOME C C++ DS Java AWT Collection Jdbc JSP Servlet SQL PL/SQL C-Code C++-Code Java-Code Project Interview (^) Custom Search S

Ea

Html Html5 CSS JavaScript Ajax JQuery AngularJS JSON GMaps Adsense Blogger Earning Email Domain SEO SMO (^) ABOUT | SERVICES | FORM S | C

JP - Java Home

-40%

Rs 475

Rs 285

JP - Synchronized Keyword

JP - Volatile Keyword

Java classes & constructor

JP - Static Block in Java

JP - Inner Classes

JP - Abstract Class

JP - Static & n-Static Variable

JP - Static & n-Static Method

JP - Constructor in Java

JP - Relationship In Java

Java Object Oriented

JP - OOP's Concept

JP - Inheritance

JP - Overloading in Java

JP - Overriding in Java

JP - Interface in Java

JP - Abstraction in Java

JP - Encapsulation in Java

JP - Polymorphism in Java

Java Advanced

JP - Package

JP - Exception Handling in Java

JP - Multithreading

JP - String Handling

JP - StringTokenizer

JP - Data Conversion

JP - Boxing and Unboxing

JP - Scanner Class

JP - Applet

JP - Factory Method

JP - Java Networking

Java Tools

Java Interview Question

Java Difference Between

System.out.println("Hello I am Static"); } } class Person { public static void main(String args[]) { A oa=new A(); oa.fun1(); // non static method A.fun2(); // static method } }

Output

Hello I am Non-Static Hello I am Static

Following table represent how the static and non-static properties are accessed in the different static or non-static method of same class or other class.

Program to accessing static and non-static properties.

Example

class A { int y; void f2() { System.out.println("Hello f2()"); } } class B { int z; void f3() { System.out.println("Hello f3()"); A a1=new A(); a1.f2(); } } class Sdemo { static int x; static void f1() { System.out.println("Hello f1()"); } public static void main(String[] args) { x=10; System.out.println("x="+x); f1(); System.out.println("Hello main"); B b1=new B(); b1.f3();