2013年9月6日星期五

java variable scope and lifecycle

1 variable scope and life cycle What is the relationship ?

2.java addition to the body and the body of the method in the class ( including parameters ) can declare variables, another location must not declare variables? Such as high-level applications where it is not there exceptions ?

3.java source code file organization must be:

class ClassName1{//类定义
    //成员变量定义
    //成员方法定义
}
class ClassName2{//类定义
    //成员变量定义
    //成员方法定义
}

//.......


This structure must be more than you ? For example, in two between the class definition you must not have other code ( except notes ) ? Class body , in addition to member variables and member methods defined herein must not have other content? Member variable and method definitions Can shuffled ?

4.JVM in the end put the memory is divided into several parts ah ? Different data says is not the same ah. .


now learning Java, the most basic sense of these concepts also need to strengthen the hope that expert guidance .
------ Solution ---------------------------------------- ----
1. variable scope and life cycle What is the relationship ?
- can be understood as basically the same, or that the decision lifecycle scope.

2.java addition to the body and the body of the method in the class ( including parameters ) can declare variables, another location must not declare variables? Such as high-level applications where it is not there exceptions ?
- where you want to declare variables? Java does not support beyond the Three Realms not the element of the so-called " global variable"

3.java source code files must be must be organized over this structure do ?
- Java after preach its object-oriented , then outside of the class is what ? What makes sense ? ( Outside the universe is what ? )
- in addition to member variables and member methods defined herein, no other up .
- defined order may be disrupted, but after all, you have to take care of the feelings of those who read it.

4.JVM in the end put the memory is divided into several parts ah ?
- this is the decision of the JVM vendor , even a different version of the JVM vendors may also be different. Do not misunderstand : JVM can not just Sun only .
------ Solution ---------------------------------------- ----


can be accessed. There are actually many people's writing habits is that all variables are declared in the class definition of the final surface .

Although the order of functions and variables can be indifferent , but also pay attention to another problem .

variable defines itself if there are dependencies , the order can not be arbitrarily disrupted , such as:
private int a = 8;
private int b = a;

static block with variable exists if dependencies , the order can not be arbitrarily disrupted , such as:
private static int a = 1;
static {
a = a * 2;
}
------ Solution ----------------------------------- ---------
◎ " but if a single form of organization from a code point of view, their status should be equal , right ? "
- scoped areas are different, Zeneng say they are equal ?
- like : one is the valid regulations, one is local regulations , the scope is actually a status symbol ah.
- only from the code in terms of organizational forms , one is outside the function {} , {} inside a function .
- another example :
public void run () {
int a = 0;
while (true) {
int b = 1;
}
System.out.println (b); / / can do it ? Because int b is deep inside { } , the function of this part is more localized
}


◎ so-called "object-oriented technology" is nothing more than " a way of organizing your code " just right
- in fact, the code is not critical , is an object-oriented design. As for how to achieve better support for object-oriented or object-oriented , each have their own practices and development.
- In addition, not all things have to be object-oriented . We often say : big and object-oriented , process-oriented small office .


◎ associated with a series of concepts of object-oriented structure ( classes, objects , encapsulation , etc. ) but we are in order to better understand this " organization code " and in the minds of people to build up a systematic logical relationship
- Exactly. Many thought , they are actually looking at how to better make it easier for the computer language with real-world business map.
------ Solution ---------------------------------------- ----

member variables relative to local variables, except the LS says national and local differences , does have some special status , member variables and objects in the life cycle of the life cycle of the same, as long as the object exist, the member variables exist, and the object can exist from the {} ( to some extent beyond the three circles , pay attention to the difference between objects and variables ) , as long as the variable that references it , it will not be the system does not recover , and local variables, only when the method is called will produce , by the end it will die. In addition , member variables can be accessed by way of reflection , but not local variables .
------ Solution ---------------------------------------- ----
as Bao said .


In fact, some things can be their own inferences , as object-oriented thinking as ours , from class to define variables ( attributes ) , is itself meaningless. Attribute must be an object's properties , if you think that an attribute can exist independently away from the object , indicating that the property itself is actually an object ( later became abstract class ) .


here stretches will involve : big and object-oriented , process-oriented small office .
because you would think , then within the function or block ( {} ) is a variable defined what ? Or it can be regarded as an object of property ?
can be considered in order to complete a task temporarily have properties , implicit attribute or combination of properties ( you swing the bat , it may be more out of a property called ball speed , this property alone or in person on the bat does not exist ) ; also can be considered a process-oriented system of thought should not care about what attributes .
------ For reference only -------------------------------------- -

Thank you Ha , another thing , since the order can be disrupted, if I declare a member variable method definition into the back , then this method can not access this variable does ?
------ For reference only -------------------------------------- -

Oh understand it , thank you very much !
then ask a question, definition of variables and methods defined in the class member variables , in addition to aspects of the different scope , but in essence, there are other differences? They are a memory area named it right ? I always feel these two positions lack a deep understanding of the variables , from the conceptual structure of object-oriented point of view, member variables should have a special status, because it is an object of the " Properties" , but if just from the code form of organization of view, their status should be equal , right ? If so , then the so-called "object-oriented technology" is nothing more than " a way of organizing your code " just right, with a series of related concepts of object-oriented structure ( classes, objects , encapsulation , etc. ) but we are in order to better understand this " organization code " and in the minds of artificially build up a systematic logic, I so understand it right ?
some ideas can not be expressed in words very clearly , I hope to understand. . . .
------ For reference only -------------------------------------- -
JAVA write-up is quite arbitrary , variable side that says this method you can also write another method or class !
------ For reference only -------------------------------------- -

Thank you very much ~
------ For reference only --------------------------- ------------
answer more detailed.
------ For reference only -------------------------------------- -

Thank supplement.
java variables , or member variables , or local variables, is that right ? There are no other possibilities ?
------ For reference only -------------------------------------- -

+ +
------ For reference only ---------------------------- -----------

no possible in java , the variables can be defined in { } , { } is either a class definition , or the method definition (or static code block initialization code block ) , that the java variables into class variables ( also more static variables ) , instance variables ( also representing member variables ) and local variables .


------ For reference only ---------------------------------- -----
2 a master 's views are well worth learning . .

still want more practice . . Landlord . After naturally understand .
------ For reference only -------------------------------------- -
1. variable scope and life cycle What is the relationship ?
- can be understood as basically the same, or that the decision lifecycle scope.

2.java addition to the body and the body of the method in the class ( including parameters ) can declare variables, another location must not declare variables? Such as high-level applications where it is not there exceptions ?
- where you want to declare variables? Java does not support beyond the Three Realms not the element of the so-called " global variable"

3.java source code files must be must be organized over this structure do ?
- Java after preach its object-oriented , then outside of the class is what ? What makes sense ? ( Outside the universe is what ? )
- in addition to member variables and member methods defined herein, no other up .
- defined order may be disrupted, but after all, you have to take care of the feelings of those who read it.

4.JVM in the end put the memory is divided into several parts ah ?
- this is the decision of the JVM vendor , even a different version of the JVM vendors may also be different. Do not misunderstand : JVM can not just Sun only .
------ For reference only -------------------------------------- -
java class support global variables , but you said java does not support global variables mean ah not quite understand
------ For reference only ----------- ----------------------------
you say java support global variables should refer to a static variable, and the above mentioned does not support global variables mean like C / C + + global variables
------ For reference only ----------------------- ----------------

java does not exist in a global variable , mainly referring to the java should be outside the body of the class can not declare a variable that is not present all locations in the program can access the variables because java is a fully object-oriented , rather than c + +, there is a process-oriented features. This is what I understand.
------ For reference only -------------------------------------- -



+ +
------ For reference only -------------------------------- -------
thank you to answer , but though it is a coincidence that gave me thinking learned ......

没有评论:

发表评论