In the jsp tutorial, there are five definitions of variables: Local range, page range, request range, session range, and application range ).
The following describes variable access methods in various technologies.
Dynamic initialization variable
Public class mainclass {
Public static void main (string args []) {
Double a = 3.0, B = 4.0;
// C is dynamically initialized
Double c = math. sqrt (a * a + B * B );
System. out. println ("hypotenuse is" + c );
}
}
Variable generation time
Public class mainclass {
Public static void main (string args []) {
Int x;
For (x = 0; x <3; x ++ ){
Int y =-1; // y is initialized each time block is entered
System. out. println ("y is:" + y); // this always prints-1
Y = 100;
System. out. println ("y is now:" + y );
}
}
}
Demonstrate how to use the correct method to declare a class variable called hellomessage
Public class mainclass
{
Static string hellomessage;
Public static void main (string [] args)
{
Hellomessage = "hello, world! ";
System. out. println (hellomessage );
}
}
Static variables
Before variables or methods, it indicates they belong to the class;
Static variables are shared among instances. If they are public static variables, other classes can access them without instantiation;
Static methods are called class methods, so they can be called without instantiation (process-oriented)
Methods of an object can access data members of an object, although they are not local variables of the method. Methods of a class can only access their own local variables.
Public class mainclass
{
Public static void main (string [] args)
{
Hellomessage = "hello, world! ";
System. out. println (hellomessage );
}
Static string hellomessage;
}
Local variable instance
Public, protected, and private modifiers for variables and methods:
Public: any other class or object that can see this class can access the data of the variable or use the method.
Public class mainclass
{
Public static void main (string [] args)
{
String hellomessage;
Hellomessage = "hello, world! ";
System. out. println (hellomessage );
}
}