Please explain this part of the method program for me? (simple beginner program)
import java.util.Scanner; public class methods { public static void main (String args []){ Scanner input = new Scanner (System.in); int total = 0 , first = 0 , second = 0 ; String operator; System.out.print("Enter first integer:"); first= input.nextInt(); System.out.print("Enter operator"); operator=input.next(); System.out.print("Enter second integer:"); second= input.nextInt(); if (operator.equals("+")==true) total= a(first, second); if (operator.equals("-")==true) total=b(first, second); if (operator.equals("*")==true) total=c(first, second); if (operator.equals("/")==true) total=d(first, second); System.out.print("result"+total); } static int result=0; static int a(int t,int y){ return result= t+y; } static int b(int t,int y){ return result= t-y; } static int c(int t,int y){ return result= t*y; } static int d(int x,int y){ return result= x/y; } } The part I dont understand is " *************static int result=0; static int a(int t,int y){ return result= t+y; } static int b(int t,int y){ return result= t-y; } static int c(int t,int y){ return result= t*y; } static int d(int x,int y){ return result= x/y; } "
static int result=0; ?? What is that ?? a method?? why couldnt i initialize it above??
It's a class variable. You can't declare it in main and then use it from your "a,b,c,d" methods.
global variable?
Nope, class variable. They're different.
ok thanks
class variable in other words is called global variable because it can be accessed in whole class
Join our real-time social learning platform and learn together with your friends!