Executing commented out functions - please explain why | Selenium Forum
M
Posted on 15/12/2016
question: In the below class car, - it has objects and a print function in main.
remaining nonstatic funcs are commented out and when this program is executed, but seeing printing its text in the console .Can anybody explain why ?
I am expecting just -4 for global variable value in the console.

thanks for your help.




// Keeping common stuff as static global
public class Car1 {
// each object of the class has all the Non Static stuff of the Class.

String model;
// making wheels common to class objects ;
//its not an object property now.
static int wheels=4;
int price;
public static void main (String[] args) {
//create 3 objects of class Car

Car c1= new Car();
c1.model="Maruthi";
c1.price=10000;
c1.start();
c1.accel();


Car c2= new Car();
c2.model="BMW";
c2.price=1000000;
c2.start();
c2.accel();

Car c3= new Car();
c3.model="coralla";
c3.price=100000;
c3.start();
c3.accel();


//System.out.println(c1.model );
//System.out.println(c2.model );
//System.out.println(c3.model );
//as it is a static var , main can access it.
System.out.println(wheels );
}


//public void start (){
//System.out.println(model+ "starting");
//}

//public void accel(){
//System.out.println(model+ "accelerating");
//}

M
Replied on 15/12/2016

If you have commented out correctly, then your code should look like below (where redlines are displayed) and you cannot run this unless these lines are commented in main function or nonstatic functions(start/accel) are uncommented

Responsive image

M
Replied on 16/12/2016

Thanks for the clarification, SK and it is working as expected with your fix.

Have one more q, please help me with this.
Before the fix,
When I ran the program,
I assume, even though the methods got executed, the console shouldn't display the result for the methods, as associated output commands (println) were commented out.


M
Replied on 18/12/2016

Non static Function/method(s) will be executed and results will be printed in console only in below cases:

1) Non static method/function(s) are called in MAIN function
2) They are NOT commented out any where in the project (either in MAIN or in child functions)

Note: Even though non static functions are called in MAIN function and if they are commented out at individual level, then no results will be displayed in console.