JAVA/J2EE/FAQ/JOB
|
|
Activity:
Question posted: 03 28 2011 12:12:36 +0000,
5 answers, 187 views, last activity
10 19 2011 13:21:01 +0000
|
|
|
|
what is efferent between using STATIC keyword with out using Static keyword in java programming language.
static means something that is shared among the objects,that is of class level.It is loaded in the memory at the compile time.Objects can't directly access it thus to access a static variable class name is used like
className.member
now problem is that main() is the entry point of the program since Java follows OOPS that's why u have to access the function using object.since object creation statement is in main() function so how JVM will access the function.For this,function is kept static so that JVM can access it without creating its object.
Mr. Jagadeesh, Many language follows the OOPs concept so one Main OOPs concept is Every things should be in a Class, nothing should be outside the class. if you don't use the STATIC keyword before Main function then you have to created the object of class, it will be outside so it destroys the OPPS concept. therefore we used the Static keyword before main (not only main in all function where we don't want to create the object of class)
static method is the Main method: When a program is launched, no instances of any class are present in memory. As the Main method is static, it can be called without creating an object and can then assume control of the program. It is the Main method's task to create the objects that the program requires to function correctly. Static Methods: Static methods are useful when creating functions that are not reliant on any instance of a class. Creating a Static Method: public static int CalculateMass(int density, int volume) { return density * volume; } Calling a Static Method: static void Main(string[] args) { int density = 50; int volume = 100; int mass = MassCalculator.CalculateMass(density, volume); Console.WriteLine("Mass: {0}", mass); // Outputs "Mass: 5000" } The static modifier can be used with classes, fields, methods, properties, operators, events and constructors, but cannot be used with indexers, destructors, or types other than classes. Reference:http://msdn.microsoft.com/en-us/library/98f28cdx(VS.80).aspx
|
|
|
|
|
|
|
|
|
|
|
|
thanks for sharing!. |
If it has changed what would be like?. |