Java static code

Tags
programming

 
Thread Tools Search this Thread
# 1  
Old 03-08-2018
Java static code

HI All

I am new to java in below code I am not able to understand the below line of code
Code:
 
 Student9.change();

the complete code is mentioned as below
Code:
class Student9{  
int rollno;  
String name;  
static String college = "ITS";  
  
static void change(){  
college = "BBDIT";  
 }  
  Student9(int r, String n){  
 rollno = r;  
 name = n;  
 }  
  void display (){System.out.println(rollno+" "+name+" "+college);}  
 public static void main(String args[]){  
Student9.change();  
 Student9 s1 = new Student9 (111,"Karan");  
Student9 s2 = new Student9 (222,"Aryan");  
Student9 s3 = new Student9 (333,"Sonoo");  
 s1.display();  
s2.display();  
s3.display();  
}  
}


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-08-2018 at 12:12 PM.. Reason: Added CODE tags.
# 2  
Old 03-09-2018
In a class, if you declare a method as static, you need not instantiate the class to be able to use the method. So , in your program, the method change() is being called directly by qualifying it with the class name.

You can read more in this link.
This User Gave Thanks to balajesuri For This Post:
dodona (03-18-2018)
# 3  
Old 04-22-2018
What bala is saying,

because not only change(), but also college, are static, they are class methods/variables rather than instance (object) methods/variables.

That means "college" will be changed for the entire class, not just for a single instance (object).

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Help with a Java code faizlo Programming 4 01-20-2014 11:14 PM
Linux through Java code Dorothy Linux 1 08-19-2012 02:18 PM
Script shell in java code chercheur857 Programming 9 05-12-2012 09:39 AM
command linux in java code chercheur857 Programming 0 04-28-2012 12:11 PM
Code java in script shell chercheur857 Shell Programming and Scripting 0 04-27-2012 06:37 PM
Script Shell in java code chercheur857 Programming 2 04-07-2012 07:17 PM
Block of code replacement in Java source code through Unix script hareeshram Shell Programming and Scripting 2 01-30-2012 11:54 AM
Even the Static cURL Library Isn't Static BrandonShw Programming 8 07-08-2011 12:57 PM
Static code analysis for Perl figaro UNIX for Advanced & Expert Users 2 05-27-2011 02:17 PM
can i have an optimal solution for this java code ? Facing Java heap space problem even at 3GB heaps vaibhavkorde Programming 2 04-12-2011 02:51 AM
Help with splitter code in JAVA eel Programming 1 09-19-2010 10:00 AM
Help with PrintWriter code in JAVA eel Programming 1 09-18-2010 07:42 AM
Help with Java code eel Programming 0 09-13-2010 11:37 AM
I need HELP to Set up Coyote Linux router with 1 static IP & 64 internal static IP dlwoaud IP Networking 0 12-21-2009 05:01 AM
Not able to send a mail using JAVA code smartgupta Programming 0 07-20-2009 10:55 AM