Ask your own question, for FREE!
Computer Science 8 Online
OpenStudy (pottersheep):

Please explain this part of the method program for me? (simple beginner program)

OpenStudy (pottersheep):

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; } "

OpenStudy (pottersheep):

static int result=0; ?? What is that ?? a method?? why couldnt i initialize it above??

OpenStudy (anonymous):

It's a class variable. You can't declare it in main and then use it from your "a,b,c,d" methods.

OpenStudy (pottersheep):

global variable?

OpenStudy (anonymous):

Nope, class variable. They're different.

OpenStudy (pottersheep):

ok thanks

OpenStudy (anonymous):

class variable in other words is called global variable because it can be accessed in whole class

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!