Friday, July 10, 2015

Instance variables

5/13/2014
public class SimpleClass {

/*variables declared in the class but not in a method that do not have the
keyword static are called instance variables*/

private int salary; /*instance variable - declared in class and outside of a method , and with no keyword - static

private String name; /*instance variable

public static void main(String[] args) {
/* sc points at instance variable */

SimpleClass sc = new SimpleClass(); /*object sc is created, blue print of class

sc.salary =30000; /*at object, variable salary has value 300000

sc.name = "Jim"; /*at object, variable name has valu Jim

System.out.println(sc.name + "makes $" + sc.salary);

}

2 comments:

  1. Why instance variables are declared private or they have to be private or what?

    ReplyDelete
  2. Are instance variables and methods different from class methods and variables?

    ReplyDelete