I got to assignment first one was to print the data of two students using different classes and constructors and count the total numbers of records and i've done this.

here is the program.

public class Student
{
  int RollNo;
  String name;
  static int stdcount=0;


  public void print(){
   System.out.println ("Roll no: " + RollNo + "" + " Name: " + name);
   }

public Student (){
  RollNo= 99999;
  name = "Name is not set";
  stdcount++;
  }

}

class Records
{
  public static void main (String args[])
  {
    Student std1 = new Student();
    Student std2 = new Student();
    std1.RollNo = 2000;
    std1.name = "Ghouri";
    std2.RollNo = 200;
    std2.name = "Ghori";
    std1.print();
    std2.print();
    System.out.println ("Total Students: " + Student.stdcount);
  }
}

in the above code, in line 5 when i m not writing static with the stdcount=0 then program is giving an error,

Student.java:32: non-static variable stdcount cannot be referenced from a static
context
System.out.println ("Total Students: " + Student.stdcount);

this is because i am referencing stdcount in a static context, but int RollNo and String name are also not static then why program is working without writing static with them ???

The second question of the Assignment is,

Calculate volume of a cube, area of a square, parameters of square and rectangle, area of a circle using constructors and different classes, set the value to 1 and define other constructors that can be call to set values of data other than 1. show the count of total number of objects and total number of general objects.


but i didn't understand the question and don't know what to do in this question :(

Recommended Answers

All 3 Replies

std1.RollNo = 2000;
Here you are referencing a class variable RollNo using an existing object: std1
Each Student object you create will have one of these variables. You need to use the object reference variable(std1) to access the variables in that object


Student.stdcount
Here you are referencing a static variable: stdcount in the class Student
This variable exists for the Class Student and all of the objects of this class share this same variable. There is only one copy for all objects, where with the class member variables, there is one per class object.

Thanks alot friend :)
and what about the second assignment question ?? do you have any idea ??

A base class of Shape extended by the shapes you are working with.

The problem statement is very ambiguous. I don't know what is being asked for.
What is the difference between an object and a general object?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.