السلام عليكم
ماهي الفائدة من استخدام static في Main Method????
السلام عليكم
ماهي الفائدة من استخدام static في Main Method????
تم تعديل هذه المشاركة بواسطة العيافـي في 13 فبراير 2011 في 09:45 — السبب: تعديل العنوان
لماذا هي static
هذا أمر له علاقة بالمنطق
الدالة main تنفذ باستخدام VM
فكيف يمكن لـ VM أن تصل إليها بدون عمل كائن من الفئة؟
لذا المصمم لجافا قرر أن تكون ثابتة static لكي يستطيع مناداتها بدون عمل كائن إضافي من الفئة
هذا ما أعتقده حول الموضوع
لكني غير متأكد
نقطة خارج الموضوع
أختي اقرأي التعليمات الخاصة بنا
واستخدمي عنوان مناسب لمحتوى موضوعك
تحياتي
قرأت أول رد ولم أقتنع به بالمرة
لأن main نفسها قادرة على أن تأخذ قيم من سطر الأوامر
وقرأت ثاني رد وكان قريباً لردي الذي رددته هنا (والذي أصبح لا يقنعني)
لكنهم يتكلموا عن main وكأنها حالة خاصة في وجهة نظري كون الدال static يعني أنها لا تشكل فرق بالنسبة للكائنات
ولم أرى أن صن خالفة القاعدة هنا
هلا قلت لي يا محمد إن كانت هناك فائدة من كون main غير ثابتة non static?
تحياتي
علاء الصالحي كتب:هلا قلت لي يا محمد إن كانت هناك فائدة من كون main غير ثابتة non static?
لم افهم سؤالك بالتحديد :lol:
---
جمعت بعض الاقتباسات قد تكون مفيدة:
اقتباسExpain the reason for each keyword of public static void main(String args[])?
public- main(..) is the first method called by java environment when a program is executed so it has to accessible from java environment. Hence the access specifier has to be public.
static: Java environment should be able to call this method without creating an instance of the class , so this method must be declared as static.
void: main does not return anything so the return type must be void
The argument String indicates the argument type which is given at the command line and arg is an array for string given during command line.
اقتباسWhat if the static modifier is removed from the signature of the main method?
Or
What if I do not provide the String array as the argument to the method?
Program compiles. But at runtime throws an error “NoSuchMethodError”.
اقتباسWhat is a static method?Static methods are implicitly final, because overriding is done based on the type of the object, and static methods are attached to a class, not an object. A static method in a superclass can be shadowed by another static method in a subclass, as long as the original method was not declared final. However, you can’t override a static method with a non-static method. In other words, you can’t change a static method into an instance method in a subclass.
اقتباسWhen is static variable loaded? Is it at compile time or runtime? When exactly a static block is loaded in Java?
Static variable are loaded when classloader brings the class to the JVM. It is not necessary that an object has to be created. Static variables will be allocated memory space when they have been loaded. The code in a static block is loaded/executed only once i.e. when the class is first initialized. A class can have any number of static blocks. Static block is not member of a class, they do not have a return statement and they cannot be called directly. Cannot contain this or super. They are primarily used to initialize static fields.
اقتباسType Life Cycle
Types are made available to a running program by the Java Virtual Machine. This is done through a process of loading, linking, and initialization. Loading is the process of locating the binary representation of a type and bringing it into the JVM. Linking is the process of taking the type and incorporating it into the runtime state of the JVM so that it can be executed. Initialization is the process of executing the initializers of a type (static initializers for classes; static field initializers for classes and interfaces). When a type becomes unreachable (i.e. the running application has no references to the type), it becomes eligible for garbage collection. This collection is called type unloading.
Loading a type, from a more detailed perspective, consists of three primary activities:
Given a type's fully qualified name, produce a stream of binary data that represents that type
Parse the stream of binary data produced in step #1 into internal structures in the method area of the JVM. The method area is a logical area of memory in the VM that is used to store information about loaded types.
Create an instance of class java.lang.Class that represents the type indicated in step #1
Further details about linking, initialization, and unloading are beyond the scope of this article. Interested readers are referred to the JVM Specification for more information.
اقتباسWhen is a Type Loaded?
This is a surprisingly tricky question to answer. This is due in large part to the significant flexibility afforded, by the JVM spec, to JVM
Loading must be performed before linking and linking must be performed before initialization. The VM spec does stipulate the timing of initialization. It strictly requires that a type be initialized on its first active use (see Appendix A for a list of what constitutes an "active use"). This means that loading (and linking) of a type MUST be performed at or before that type's first active use.
اقتباسwhy main method is declared static in java?
We declare, main method in java as : public static void main(String args[])
public : means it can be accessible to all other classes.
static : main is the entry point of a class. In java everything thing is written in a class.Now when you run java filename on command prompt, loader will load the class and jvm will search the main method to enter into the class. so making the main() as static, will make jvm access it directly through classname.main()
Now once jvm get the main(), object instantiated there are accessed.
void : main method should return anything.
main : its the method name, the JVM will seek for after the class is loaded on runing command like e.g java classname
String args[] : String array named args used to access the string variables passed on command line. e.g java classname firstname lastname
اقتباسThe public static void keywords mean the Java1 virtual machine (JVM) interpreter can call the program's main method to start the program (public) without creating an instance of the class (static), and the program does not return data to the Java VM interpreter (void) when it ends.
From SUN Docs
روابط الاقتباسات:
http://java.sun.com/developer/onlineTraining/Programming/BasicJava1/prog.html
http://amijha.wordpress.com/2008/06/06/why-main-method-is-declared-static-in-java/
http://www.developer.com/java/other/article.php/2248831/Java-Class-Loading-The-Basics.htm