• No se han encontrado resultados

CERRAMIENTOS, PASOS Y PASTORES ELÉCTRICOS

{ string sname; int rollno; string coll-name; }

- In the above class, even though u create a lot of objects, all of them having same coll- name duplicated in all object which is of no use. So, U maintain that value in a single variable of type ‘static’ which is a class level variable.

- If the value of a variable, is same for all objects, such type of variables not suggestable to declare at object level. We have to declare these variables at class level so that a single copy is shared by all instances. Such type of variable we have to declare by using keyword “static”.

- i.e. Instance variables are object level variables but static variables are class level variables.

Note :

Don’t assume, because static variables r declared within class they r called as “class variables”. Because, though instance variables r created within the class – they r called as “instance variables” but not class level variables.

Conclusions reg static variable :

1. In case of static variables, a single copy will be created and all the objects will share that variable which is very useful in view of memory.

2. Static variables can be accessed with class name as well as object reference.

By using object reference, if a person changed the value of a static variable, these changes will be reflected for all the objects. To prevent this, most of the cases, static variables are always associated with “final” keyword.

3. The static variables will be created while the class loaded into the memory and destroyed while unloading the class from the JVM Memory. I.e. static variables will exist as long as the corresponding class present in JVM memory.

4. A single global copy is created for static variables which can be accessed by remaining all objects.

5. In case of static variables (As similar to instance variables), default values will be assigned if u r not performing any explicit initilization.

Eg : Class Sample {

Static int i;

P S V M (S[ ] args) {

Sample S = new sample ( );

Sopln (s.i); // legal but not recommended Sopln (sample. i); // legal & recommended Sopln (i); } } O/P : O O O

Local Variables :

- Also known as “Temporary Variables” (or) “Stack Variables” (or) “Automatic Variables”.

- Within a block if we declare any variable, for temporary purpose, such type of variables are called “Local variables”. The Scope (where these can be present) of is where they are declared within a block.

Eg : class sample { P.S.V.M (S[ ] a) { int i; Sopln (i); } } O/P :

Comp. Error : Variable ‘i’ might not have been initialized.

- For the local variables, there won’t be the concept of “Default Initilization”. It is programmer’s responsibility to perform initialization for local variables explicitly. - Before using a local variable, we should perform initialization, otherwise compile

time error. Eg : class sample { P.S.V.M (S[ ] a) { int i;

Sopln (“Don’t Sleep”); }

}

Eg : class sample { P.S.V.M (S[ ] a) { int i; if (args.length > 0) { i = 10; } Sopln (i); } }

O/P : Comp. error : V ‘i’ m n h i.

Note : Initialization is not suggestable in the conditional blocks. Eg : class sample { P.S.V.M (S[ ] a) { int i; if (args.length > 0) { i = 10; } else; { i = 20; } SOP (i); } }

Conclusions :

- If U R not performing initialization, for the static and instance variables, default values will come. But for the local variables, default values will come. But for the local variables, there is no concept of default initialization we should perform initialization before using local variable.

- Local variables, the only allowed modifier is “final”. i.e. the following local variable declarations.

Invalid Error : illegal start of expression Public int i = 10; final int i = 10;

Private int i = 10; int i = 10; Static int i = 10;

- Local variables can’t be static because static variables can be accessed with classname and object of class outside the methods also, but local variables can’t be used outside the block.

Un initialization in Arrays : Eg : class sample { int [ ] a; P S V M (S[ ] a) {

Sample S = new sample ( ); Sopln (s.a.); // null

Sopln (s.a[ 0 ]); // Null pointer exception (Run time exception) }

}

At the instance level :

1. int [ ] a sopln (obj. a); null. sopln (obj. a [ 0]); NPE 2. int [ ] a = new int [ 6 ]; sopln (obj.a); i@ 12D34

Static :

1. int [ ] a; sopln (a); null. sopln (a [ 0]); NPE 2. int [ ] a = new int [ 6 ]; sopln (a); i@ 1add 2dd

sopln (a[ 0]); O

‘Local’ level :

1. int [ ] a; sopln (a); CE

No need of its components 2. int [ ] a = new int [ 4 ]; sopln (a); // i@ 1add 2dd

sopln (a[ 0]); O

- Whether an array is declared instance static (or) local its elements always assigned with default values.

When arrays r declared at static / instance level, V can print the address (reference) of an array object as null, but V can’t print a3[ 0 ], results in CJE because null object points to nothing.

Where as if V declare an array as local, then, V can’t print reference of an array object, Vget CTE : variable <array> might not have been initialized.

Signature (or) Complete Declaration of Main Method :

- Public static void main (String[ ] args) {

--- --- }

Public : To call this method by JVM from anywhere (JVM is configured in such a way that it finds the main method in its defined signature.

Static : Without object also we can in invoke this method.

Void : This method doesn’t return anything to JVM.

Main : Name of the method.

String[ ] args : Command line arguments array.

* If there is no main method, in run-time while executing we get error : No such method error.

Different cases w.r.t. main ( ) : 1. class test { --- --- } > javac test

> java test No such method error.

- Compiler never checks whether class contain main method or not but at run-time JVM checks for the main method with the specified signature. If it is not finding any method with the specified signature, it lises “no-such method” error.

Note : It is run-time error only, but not compile time error. 2. Class test

{

Private static void main (String[ ] args) {

--- --- } }

Run – time error : Main Method not public.

In java, a method is not allowed to be declared within another method. 3. Class test

{

Public void main (String[ ] args) {

SOP (“…….”); }

}

4. Class test {

Public static int main (String[ ] args) {

SOP (“…….”); return 10; }

}

RJE : No such method error.

5. Class test {

Public static void main (String[ ] args) {

SOP (“…….”); }

}

NO CTE, RTE; No such method error.

Exercise : (w.r.t. main( ) method)

1. Public static void main (String[ ] args) { } 2. Protected static void main (String[ ] args) { }

RTE : ‘Main’ not public RTE : No Such Method Error

3. Public void main (String [ ] args) { } 4. Public static int main (String [ ] args)

{

return 10; }

RTE : No such method error.

5. Public static void MAIN (String[ ] args) { } RTE : No such method error.

6. Public static final void main (String [ ] args) { }

7. Public static synchronized void main (String [ ] args) { } 8. Public static void main (String args) { }

RTE ; No such method error.

9. Public static void main (String [ ] Pavan Kiran) { } 10. Public static void main (String …… args) { }

Command – line arguments :

- Arguments passed during run-time in command prompt Eg : java test arg [ 0 ] [ 1 ] [ 2 ] [ 3 ]

10 20 30 40 Command-line arguments If U access args [ 4 ] – error.

- args [ 0 ] – 1st Gmd line argument

args [ 1 ] – 2nd Gmd line argument

args.length – the no. of command-line args.

Args [ 4 ] – 5th Gmd line arg, but there is no such args results in

“Array Index Out Of Bounds” Exception. - WAP to display all Gmd and line args.

Class test {

P S V M (S[ ] a) {

for (int i = o; i < args.length; i + +) S.O.Pln (i);

} }

Public, Static, Void } Keywords String } Class name Args, Main } Not keywords

Eg : Public class A {

P S V M (S[ ] args) {

String [ ] [ ] argcopy = new string [ 2 ] [ 2 ]; int x;

argcopy [ 0 ] = args; x = argcopy [ 0 ].length; for (int y = 0; y<x; y ++) { SOP (“ “ +argcopy[ 0 ] [ y ]); } } } java A 1 2 3 O/P : 1 2 3

Documento similar