#38 Static Method in Java

  Рет қаралды 139,900

Telusko

Telusko

Жыл бұрын

Check out our courses:
Enterprise Java Spring Microservices: go.telusko.com/enterpriseJava
Coupon: TELUSKO10 (10% Discount)
Master Java Spring Development : bit.ly/java-spring-cloud
Coupon: TELUSKO20 (20% Discount)
For More Queries WhatsApp or Call on : +919008963671
website : courses.telusko.com/
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; }
b) before a block class Calc{ static {System.out.println("this is static block);}}
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
System.out.println(Calc.stA); //use with class name
System.out.println(obj1.stA); //use by object name
//for instance variable
//System.out.println(Calc.inB); //got an error --Cannot make a static reference to the non-static
System.out.println(obj1.inB); //used by object name reference
obj1.inB=1000;
obj1.stA=2000;
//static
System.out.println(obj1.stA);//output: 2000
System.out.println(obj2.stA);//output: 2000 value changed for both obj1 and obj2
//instance
System.out.println(obj1.inB); // output: 1000
System.out.println(obj2.inB); // output: 100 no change in obj2
}
}
#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{
System.out.println("Static Block");
System.out.println("Executed before main");
}
{
//non static block
System.out.println("Non static block executed before the execution of constructor");
}
public static void main(String []args){
System.out.println("main method");
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
Help.wish(); //-- this will execute static block of Help class
}
}
class Help{
static {
System.out.println("Static block of Help class");
}
static void wish(){
System.out.println("Hello");
}
}
/*
PS D:\telusko\january\java-course-telusko\code javac Calc.java
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 we can call static method with object reference or class name e.g ClassName.staticMethod() or objReference.statiMethod()
= but non static method dependent on object so it can be called only by object reference. e.g objReference.nonStaticMethod();
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.
Github repo : github.com/navinreddy20/Javac...

Пікірлер: 48
@legendo72
@legendo72 11 ай бұрын
thanks dear: you nailed it...... you don't know how much search i did about static method.... even my university teachers did not given me a satisfied answer...
@neerajgarg9096
@neerajgarg9096 10 ай бұрын
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
@mathsforyou9198
@mathsforyou9198 7 ай бұрын
Jayada ho gaya bhai
@HaiderAli-py1sy
@HaiderAli-py1sy 5 ай бұрын
my teacher should resign after watching your video
@tsgaming9635
@tsgaming9635 Ай бұрын
True bro 😢
@harrisonian9213
@harrisonian9213 Жыл бұрын
Awsone video you've made. Really great and easy to understand with your skillful explanation. Thumbs up!!
@KODAhmed
@KODAhmed 4 ай бұрын
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
@chinnijyothiprakash9249
@chinnijyothiprakash9249 14 күн бұрын
The explanation of the reason behind making the main function static is top-notch.
@rajansharma9066
@rajansharma9066 3 ай бұрын
What an explanation, great, bravo !!!!
@yuv09
@yuv09 Жыл бұрын
Great Video Sir, Simple and Clear
@deepshah2305
@deepshah2305 Жыл бұрын
Excellent explaination!!
@MsSharayu
@MsSharayu Жыл бұрын
Great Video!
@avadheshkumar1488
@avadheshkumar1488 10 ай бұрын
Superb explanation 👌 👏 👍
@Rayhan1f
@Rayhan1f Жыл бұрын
Awesome 💙💙. You are a great explanation ability in especially Java, I salute to you, sir, 🫡🫡🫡🫡
@MR-vi2wk
@MR-vi2wk 5 ай бұрын
Best video on this topic so far, thanks will safe my exam ;)
@its_SR07
@its_SR07 Жыл бұрын
amazingly explained
@rajithasri8426
@rajithasri8426 Жыл бұрын
Great tutorial
@nononnomonohjghdgdshrsrhsjgd
@nononnomonohjghdgdshrsrhsjgd 7 ай бұрын
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?
@sudhanshugorwadkar3839
@sudhanshugorwadkar3839 6 ай бұрын
Superb explanation for why the main method is static💯
@-MShivakumar
@-MShivakumar Жыл бұрын
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 . ???
@moessyd
@moessyd Ай бұрын
Man thank you ❤🔥
@studywithassu666
@studywithassu666 Жыл бұрын
Finally 🎉 understood
@FatesRePlay
@FatesRePlay Жыл бұрын
Execute the program when you make changes?
@Sanjiiiiiiiiivani
@Sanjiiiiiiiiivani Жыл бұрын
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
@BIT_RaginiV
@BIT_RaginiV 3 ай бұрын
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
@newmoviesthamizhantech2663
@newmoviesthamizhantech2663 6 ай бұрын
Can u anyone explain what he is lectured the static methods? I can't understand
@ItsPouring
@ItsPouring 3 ай бұрын
I'm not "too dumb" to get it. Thank you, heart of gold!! 💖👍
@madhanmullapudi3705
@madhanmullapudi3705 5 ай бұрын
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??
@rushikeshw8368
@rushikeshw8368 Жыл бұрын
what is the reason behind using static method??
@SIMEBRHruthik
@SIMEBRHruthik 10 ай бұрын
girlfriend is like non static method but sister is like static method, we call directly .
@ankushbhagat8434
@ankushbhagat8434 6 ай бұрын
Yeah we need object (might be sister's friend ).
@Nitishrajrocks
@Nitishrajrocks Жыл бұрын
This is why we use Static in main method 3:31
@rakhamajighagare
@rakhamajighagare Жыл бұрын
can we access static method by using object reference?
@studywithassu666
@studywithassu666 Жыл бұрын
Yes
@kshitizmayank7208
@kshitizmayank7208 11 ай бұрын
Shouldn't it be mobile obj1 in show1 method rather than mobile obj
@jaykeranthony9255
@jaykeranthony9255 9 ай бұрын
thts what im confused with too
@avantirao3140
@avantirao3140 6 ай бұрын
its just a passed as a parameter and you can name the parameter anyway you want
@puruagni1927
@puruagni1927 3 ай бұрын
Why are you not updating VS Code? Please update VS Code.
@user-benjaminJohn
@user-benjaminJohn 9 ай бұрын
Hi Navin, did you just create a Main method with (String a [ ]) instead of (String [ ] args)? When did this happen? Should I stop watching the videos out of order?! Keep up the good work.
@yashaswinihm4288
@yashaswinihm4288 7 ай бұрын
I think both (String a[ ]) and (String[ ] a) are allowed.we can use any one . Ans from chatgpt
@devendrachaudhari5687
@devendrachaudhari5687 6 ай бұрын
Yes you can use bz a and args are just reference to these String array . You can also use in reference name like (String b[ ]) these array is use for take command line arguments
@pragu___official399
@pragu___official399 5 ай бұрын
I think u can use String a[] but u should not use that a variable anywhere in the main method
@krishnarao3933
@krishnarao3933 7 ай бұрын
But how can a main method gets executed without calling it? Its a little bit confusing🤦
@yashaswinihm4288
@yashaswinihm4288 7 ай бұрын
I think there is no need to call the main method. It will execute without calling
@reshmalal4024
@reshmalal4024 6 ай бұрын
JVM calls the main method
@kvelez
@kvelez 10 ай бұрын
public class Main{ public static void main(String[] args) { Mobile obj1 = new Mobile("Apple", 1500, "Smartphone"); obj1.Show();//Method call Mobile.display(obj1);//Static call } } class Mobile { //We could make attributes static String brand; int price; String name; public Mobile(String brand, int price, String name){//Constructor this.brand = brand; this.price = price; this.name = name; } public void Show(){ System.out.println(name + " " + brand + " " + price); } //Call normal variables using the obj public static void display(Mobile obj){ System.out.println(obj.name + " " + obj.brand + " " + obj.price); } }
@garvisrobot9274
@garvisrobot9274 Жыл бұрын
Great
@aruljebin
@aruljebin Жыл бұрын
Sir, this video is incorrectly number.. please correct it
#39 Static Block in java
7:17
Telusko
Рет қаралды 103 М.
#40 Encapsulation in Java
11:42
Telusko
Рет қаралды 157 М.
小宇宙竟然尿裤子!#小丑#家庭#搞笑
00:26
家庭搞笑日记
Рет қаралды 17 МЛН
Son ❤️ #shorts by Leisi Show
00:41
Leisi Show
Рет қаралды 8 МЛН
Inside Out Babies (Inside Out Animation)
00:21
FASH
Рет қаралды 23 МЛН
#4.6 Java Tutorial | Static Keyword
12:20
Telusko
Рет қаралды 451 М.
#37 Static Variable in Java
7:06
Telusko
Рет қаралды 133 М.
Stop, Intel’s Already Dead!
13:47
Linus Tech Tips
Рет қаралды 794 М.
Java Strings are Immutable - Here's What That Actually Means
7:06
Coding with John
Рет қаралды 609 М.
JPEG is Dying - And that's a bad thing
8:09
2kliksphilip
Рет қаралды 165 М.
Static vs Non-Static Variables and Methods In Java - Full Simple Tutorial
11:29
Java Main Method Explained - What Does All That Stuff Mean?
7:10
Coding with John
Рет қаралды 225 М.
#51 This and Super Method in Java
12:11
Telusko
Рет қаралды 97 М.
Это iPhone 16
0:52
Wylsacom
Рет қаралды 1,3 МЛН
Klavye İle Trafik Işığını Yönetmek #shorts
0:18
Osman Kabadayı
Рет қаралды 9 МЛН
Запрещенный Гаджет для Авто с aliexpress 2
0:50
Тимур Сидельников
Рет қаралды 1,1 МЛН
Проверил, как вам?
0:58
Коннор
Рет қаралды 413 М.
Tag him😳💕 #miniphone #iphone #samsung #smartphone #fy
0:11
Pockify™
Рет қаралды 4,8 МЛН