Results 1 to 3 of 3
  1. #1
    kashyapkeshav is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default needed clarificarions on constructors

    Hi all,

    I need to understand why the arguments passed in the constructor is not accessible ?

    in the below program i had to refer the variable 'J' to another variable 'I' in
    order to operate it ?

    ie : Constrct (int j){
    i=j;

    2. in the program when i executed the the statement "System.out.println("Constrct value j is "+C);"
    i could see the memory location as output

    for eg : Constrct value j is com.java2.Constrct@5224ee

    How the memory mapping happens in the result . please help me finding out the solution.


    package com.java2;

    public class Constrct {
    int i,k;
    }

    Constrct (int j){
    i=j;
    class B2{
    public static void main(String[] args){

    System.out.println("Constructor demonsration");,
    Constrct C= new Constrct(23);
    System.out.println("Constrct value j is "+C);
    System.out.println("Constrct value j is "+C.i);
    }

    In the

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: needed clarificarions on constructors

    What you are seeing printed is what is returned by the default toString() method (from the Object class).
    If you want to see something else, override the class's toString() method and have it return the String you want to see.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    b4viral is offline Member
    Join Date
    Jul 2012
    Posts
    17
    Rep Power
    0

    Default Re: needed clarificarions on constructors

    the output of the snippet you pasted will be as following

    Constructor demonsration
    Constrct value j is Constrct@2e0b08f1
    Constrct value j is 23

    the address is the reference address where the memory is allocated for that particular object 'C'
    as class Construct is having 2 integer member, at the time of object creation [new Construct(23)] memory(which can contain 2 integer values) will be allocated and the reference variable will be pointing to that location so if you print reference, it will print address[as in red above] and if you access any specific variable using that reference and print it will print the value[as in green above]

Similar Threads

  1. needed clarificarions on constructors
    By kashyapkeshav in forum New To Java
    Replies: 2
    Last Post: 08-18-2012, 01:00 PM
  2. Constructors
    By cups in forum New To Java
    Replies: 1
    Last Post: 02-15-2012, 11:55 AM
  3. Constructors
    By EdOBannon in forum Advanced Java
    Replies: 2
    Last Post: 12-15-2011, 11:05 AM
  4. Constructors
    By new2java2009 in forum New To Java
    Replies: 5
    Last Post: 08-18-2009, 06:46 AM
  5. constructors
    By khamuruddeen in forum New To Java
    Replies: 2
    Last Post: 12-01-2007, 03:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •