Chapter3_2-ObjectsAndInstanceAtributes

Class attributes and methods can also be manipulated

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: the class shares class attributes with the other objects. • Any object of the class can change the value of a class attribute. • Class attributes (and methods) can also be manipulated without creating an instance of the class. Page 20 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Class Attributes Declaration • The class attributes (and methods) are declared as instance attribute but with the static modifier in addition. <modifiers> Modifiers Modifiers public static Page 21 <data type> <attribute name> ; Data Type Data Type int Name Name studentNumber ; Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP Class Attributes Access • Class attributes (and methods) can also be manipulated without creating an instance of the class. <class name>.<attribute name> Class Name Class Name Attribute Name Attribute Name Course.studentNumber = 0 ; Page 22 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP class Course { // attributes public String studentName; public String courseCode ; public static int studentNumber; } public class CourseRegistration { public static void main(String args) { Course course1, course2; //Create and assign values to course1 course1 = new Course( ); Course.studentNumber = 1; course1.courseCode= new String(“CSC112“); course1.studentName= new String(“Majed AlKebir“); //Create and assign values to course2 course2 = new Course( ); Course.studentNumber ++; course2.courseCode= new String(“CSC107“); course2.studentName= new String(“Fahd AlAmri“); System.out.println(course1.studentName + " has the course “+ course1.courseCode + “ ” + course1.studentNumber); System.out.println(course2.studentName + " has the course “+ course2.courseCode + “ ” + course2.studentNumber); } } Page 23 Dr. S. GANNOUNI & Dr. A. TOUIR Introduction to OOP...
View Full Document

This document was uploaded on 03/03/2014.

Ask a homework question - tutors are online