473,387 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

problem with constructor

I was not understanding what is going on the output of below code is 0 0.But i was expecting it to be 45 12.Can someone explain the reason for such output.


Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.io.*;
  3. import java.lang.*;
  4. class person{
  5. static int pn,py;
  6. }
  7. class patient extends person{
  8. public patient()
  9. {
  10. pn=45;
  11. py=12;
  12. }
  13. static void fun()
  14. {
  15. System.out.println(""+pn+py);
  16. }
  17. public static void main(String args[])
  18. {
  19. fun();
  20. }
  21. }
Aug 15 '15 #1

✓ answered by Mator

You first have to instantiate your patient class
The assignment is made in the constructor of the patient class is it not?

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.io.*;
  3. import java.lang.*;
  4.  
  5. class person{
  6.     static int pn;
  7.     static int py;
  8. }
  9.  
  10. class patient extends person{
  11. public patient(){
  12.     person.pn = 45;
  13.     person.py = 12;
  14. }//end of patient constructor
  15.  
  16. static void fun(){
  17.     System.out.println("" + person.pn + patient.py);
  18. }//end of fun()
  19.  
  20. public static void main(String args[]){
  21.     patient pat = new patient();
  22.     fun();
  23. }
  24. }//end of patient class
  25.  
  26.  
  27.  
You also don't need to import java.lang (always implicitly imported by the JVM)

3 1270
Mator
3
You first have to instantiate your patient class
The assignment is made in the constructor of the patient class is it not?

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import java.io.*;
  3. import java.lang.*;
  4.  
  5. class person{
  6.     static int pn;
  7.     static int py;
  8. }
  9.  
  10. class patient extends person{
  11. public patient(){
  12.     person.pn = 45;
  13.     person.py = 12;
  14. }//end of patient constructor
  15.  
  16. static void fun(){
  17.     System.out.println("" + person.pn + patient.py);
  18. }//end of fun()
  19.  
  20. public static void main(String args[]){
  21.     patient pat = new patient();
  22.     fun();
  23. }
  24. }//end of patient class
  25.  
  26.  
  27.  
You also don't need to import java.lang (always implicitly imported by the JVM)
Aug 15 '15 #2
could you explain what you said
Aug 15 '15 #3
Mator
3
I have added code samples to make it clearer.
Aug 15 '15 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Amadrias | last post by:
Hi all, I am using a class to transport some data over the network. I then added the attribute to the class. My problem is that this class is part of a framework and that I do not want...
16
by: yuraukar | last post by:
I am trying to create a garbage collection class in C++ to collect instances of a specific class (so no general gc). The approach is to use smart pointers and a mark and a simple sweep gc. ...
6
by: Nafai | last post by:
Hello. I want to do something like this: class A { // It's virtual protected: float* data; int n; public: A(int a); virtual float* createData(); //...
19
by: Martin Oddman | last post by:
Hi, I have a compiling problem. Please take a look at the code below. I have an application that is built upon three tiers: one data tier (Foo.DataManager), one business tier (Foo.Kernel) and...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
23
by: TarheelsFan | last post by:
What happens whenever you throw an exception from within a constructor? Does the object just not get instantiated? Thanks for replies.
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
9
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.