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


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";
    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
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 :(

Edited by Xufyan: n/a

7 Years
Discussion Span
Last Post by NormR1

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

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.

Edited by NormR1: n/a


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?

Edited by NormR1: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.