#38 Static Method in Java

preview_player
Показать описание
Check out our courses:

Coupon: TELUSKO10 (10% Discount)

Coupon: TELUSKO20 (20% Discount)

For More Queries WhatsApp or Call on : +919008963671

In this lecture we are discussing:
1)What is use static keyword in java? (discussed in static variable lecture)
2)At which place we can use static keyword.
3)when memory get allocated (discussed in static variable lecture)
4)static variable vs instance variable(discussed in static variable lecture)
5)static block vs non static method (discussed in static block lecture)
6)static method vs non static method (discussed in this lecture)

#1
Use of static keyword
= when we are using static keyword with variable, block, method it become
independent of object.
e.g class{
static int a=5;
}
= a is not dependent on object .if we have two object obj1 and obj2 then both object able to access this
variable.
= when we are using static it become independent to object.

#2
Four place we can use static keyword
a) before variable declaration e.g class Calc{ static int s; }
c) during method creation class Calc{public static void add(int num1,int num2){};}
d) with concept of inner class (It is not discussed in this lecture)
class OuterClass {
int x = 10;
static class InnerClass {
int y = 5;
}
}
#3
When memory get allocated
= for that we need to know some term
a)stack area b)heap area c)class loader system
Step 1: when you compile the code you get .class file
Step 2: when you are executing (java MainClass) first class loading to class loader System.
Step 3: During class loading static variable initialize, static block get executed.
Step 4: Since, static variable got memory in heap before object creation. Now we can say that it is independent of object.

#4
Static variable vs instance variable

Class Calc{
static int stA=100; //independent of object // we can use this by class name as well as object
int inB=100; //dependent of object //we can only use this by object

public static void main(String []args){
Calc obj1=new Calc();
Calc obj1=new Calc();
//for static variable

//for instance variable

//static

//instance
}
}

#5
static block vs non static block
= remember static block executed before the execution of static method
= non static block executed when you try to create the object and executed before constructor called.

class Calc{
static{
}

{
//non static block
}

public static void main(String []args){
Calc c = new Calc(); //non static block executed when we create object
// Help h ; -- this will not execute static block of Help class
// Help h = new Help(); this will execute static block of Help class

}
}

class Help{
static {
}
static void wish(){
}
}

/*
PS D:\telusko\january\java-course-telusko\code java Calc
Static Block
Executed before main
main method
Non static block executed before the execution of constructor
Static block of Help class
Hello
*/

#6
Static method vs non static method

Remember one thing:
i)we can use static property, member inside non static block or method without object creation.
ii) But we cannot use non static property or method inside static block or method without object creation.

Рекомендации по теме
Комментарии
Автор

thanks dear: you nailed you don't know how much search i did about static method.... even my university teachers did not given me a satisfied answer...

legendo
Автор

The explanation of the reason behind making the main function static is top-notch.

chinnijyothiprakash
Автор

Awsone video you've made. Really great and easy to understand with your skillful explanation. Thumbs up!!

harrisonian
Автор

brother. you are a genious at explaining stuff DAAAAMN... if only i met your chanell earlier you would have helped me so much in my programming carrer your AMAZING

KODAhmed
Автор

watched all previous videos but i liked this one because i feel goosebumps when you explain why we use static in starting :) love your content <3

neerajgarg
Автор

my teacher should resign after watching your video

HaiderAli-pysy
Автор

Best video on this topic so far, thanks will safe my exam ;)

MR-viwk
Автор

What an explanation, great, bravo !!!!

rajansharma
Автор

Awesome 💙💙. You are a great explanation ability in especially Java, I salute to you, sir, 🫡🫡🫡🫡

Rayhanf
Автор

Superb explanation for why the main method is static💯

sudhanshugorwadkar
Автор

you know it is showing null in the output for the static instance variable category but when we pass the value in the class like String category = " Smartphones" ; it 's not showing null iin that

if having a explain please explain me

Sanjiiiiiiiiivani
Автор

passing reference obj for one time how it will print the object 2 data .? we do here mobile.show1(obj2) how it will print the data we only pass one time reference only as obj in show method we don't pass obj2 we just created obj2 . ???

-MShivakumar
Автор

ok. it sounds reasonable. since starting point for execution is main(), the method should be static, so that no instance of a class is created. Is the method main() not predefined in some initial class or is for JVR only the name main() important?

nononnomonohjghdgdshrsrhsjgd
Автор

I'm not "too dumb" to get it. Thank you, heart of gold!! 💖👍

ItsPouring
Автор

Execute the program when you make changes?

FatesRePlay
Автор

Can u anyone explain what he is lectured the static methods? I can't understand

newmoviesthamizhantech
Автор

to call main, we no need to create object but to access static method, we need to use classname.methodname() right....then y we are not doing that

BIT_RaginiV
Автор

This is why we use Static in main method 3:31

Nitishrajrocks
Автор

what is the reason behind using static method??

rushikeshw
Автор

You said that we cant able to call non static variables in static methods. But main is also a static method right. Then how can we able to call non static variables in main method. Can somebody explain??

madhanmullapudi