"If there are several visible (accessible) variables in our code – say, an instance variable and a local variable – the local variable will be accessed."
class Main
{
public int count = 0; // Declare an instance variable
public static void main(String[]args){
Main mn = new Main();
mn.run();
}
public void run()
{
count = 15; // Access the instance variable
int count = 10; // Declare a local method variable
count++; // Access the method variable
System.out.println(count);
}
System.out.println(count);
}
( Lesson 1 Level 4 ) Tried to execute the code snippet on given concept as follows:- But didn`t get how to play with it programmatically
Under discussion
Comments (11)
- Popular
- New
- Old
You must be signed in to leave a comment
Misiu
30 October 2020, 20:38
Move last print to main() method.
Use println(mn.count) - object variable or make class's variable count static - class variable.
0
Dinesh
30 October 2020, 21:18
Thanks It worked
One question please help
in the code snippet
public void run() method
why it is not giving an error though we have not declared the type of count as int count
I mean in
public void run()
{
count = 15; // Access the instance variable
//why we have not given data type to the count variable?
}
0
Misiu
30 October 2020, 21:27
In the line (at beginning of the code):
public int count = 0; // Declare an instance variable
0
Dinesh
30 October 2020, 21:57
Going through the lesson known about
1. Instance variable -> belongs to each object
2.Class Variable -> belongs to class
3. Local variable -> belongs to method
4. Argument in a method is called as local variable
I have confusion between these two
1.Member variable
2.Fields
And if argument in a method call is called as local variable than what does parameter in the method declaration called as?
0
Misiu
30 October 2020, 22:07
Sorry, but my English can't catch the last sentence.
Maybe give an example.
0
Dinesh
30 October 2020, 23:22
About last sentence
Consider method m1()
m1(26, Jack) // method call
-> 26 and Jack are called as arguments
and these(26 and Jack) are local variables in a method call m1()
m1(int number, String name) // method declaration
--> int number and String name are parameters
(int number and String name) what ? should I call them in method declaration.
(int number and String name act as local variable or or any thing else)?
0
Misiu
31 October 2020, 00:01
I don't know if I understand you questions...
26 and Jack are not variables. They are values of variables.
number and name are local variables. When method m1() ends its work, they are disapearing, deleted.
0
Dinesh
31 October 2020, 05:20
Yes of course you understood my question very well
I was asking this stuff as somewhere I had read that argument in methods act as local variable.
But you explained that not the arguments but the parameters act as local variable.
Now I got the answer. Am I right?
Thank You
0
Misiu
31 October 2020, 14:55
In courses, books there are some misunderstandings - argument/parameter.
After calling m1(26, Jack): 26/number and Jack/name become the same local things - variables which have passed arguments (values).
0
Dinesh
31 October 2020, 15:43
What are Actual and Formal Arguments
I got this error in a program
Attachment image:
0
Misiu
31 October 2020, 16:17
Formal argument - declared variable
Actual argument - value of the variable
Your print() method looks for some value but doesn't find it. There is nothing passed to print() method. But it looks that something should be passed to it.
0