Menu

Post image 1
Post image 2
1 / 2
0

Static and NOn-static

DEV Community: java·Bala Murugan·2 days ago
#D3Nk1pd0
#dev#student#static#name#variable#fullscreen
Reading 0:00
15s threshold

static variable : *A static Variable belongs to the class, not to an object. nly one copy is created and shared by all objects. Example: class Student { static String college = "Bala" ; } public class Main { public static void main ( String [] args ) { Student s1 = new Student (); Student s2 = new Student (); System . out . println ( s1 . college ); System . out . println ( s2 . college ); } } Enter fullscreen mode Exit fullscreen mode Output: Bala Bala Enter fullscreen mode Exit fullscreen mode Non-Static Variable (Instance Variable): A non-static variable belongs to an object. Every object gets its own copy of the variable. Example: class Student { String name ; } public class Main { public static void main ( String [] args ) { Student s1 = new Student (); Student s2 = new Student (); s1 . name = "Bala" ; s2 . name = "Kumar" ; System . out . println ( s1 . name ); System . out . println ( s2 .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More