Unix/Linux Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Java static code

Programming


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-08-2018   -   Original Discussion by scriptor
scriptor's Unix or Linux Image
scriptor scriptor is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 25 May 2018, 7:08 AM EDT
Posts: 181
Thanks: 21
Thanked 0 Times in 0 Posts
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:
Java static code Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-08-2018 at 11:12 AM.. Reason: Added CODE tags.
Sponsored Links
    #2  
Old Unix and Linux 03-08-2018   -   Original Discussion by scriptor
balajesuri's Unix or Linux Image
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 12 May 2018, 7:02 AM EDT
Location: India
Posts: 1,984
Thanks: 31
Thanked 575 Times in 548 Posts
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.
The Following User Says Thank You to balajesuri For This Useful Post:
dodona (03-18-2018)
Sponsored Links
    #3  
Old Unix and Linux 04-22-2018   -   Original Discussion by scriptor
dryden's Unix or Linux Image
dryden dryden is offline
Registered User
 
Join Date: Apr 2018
Last Activity: 30 April 2018, 11:29 AM EDT
Posts: 8
Thanks: 0
Thanked 2 Times in 2 Posts
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).
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Block of code replacement in Java source code through Unix script hareeshram Shell Programming and Scripting 2 01-30-2012 10:54 AM
Even the Static cURL Library Isn't Static BrandonShw Programming 8 07-08-2011 11:57 AM
Static code analysis for Perl figaro UNIX for Advanced & Expert Users 2 05-27-2011 01: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 01:51 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 04:01 AM



All times are GMT -4. The time now is 10:52 PM.