Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 28

Thread: hello

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default hello

    hello everyone.

    I tried to find out correct section to ask this question,but i could not find way. could you suggest with answer?

    my code:

    class a{
    public static void main(String args[]){
    int i=10;
    System.out.println(i);
    }
    class b{
    public void max(){
    System.out.println("i");
    }
    }
    }

    my q: i want printout of class b. what is wrong?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: hello

    What would a "printout of class b" look like? What does your code print now?
    Post the program's output and explain what is wrong with it and show what you want the output to look like.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: hello

    Quote Originally Posted by Norm View Post
    What would a "printout of class b" look like? What does your code print now?
    Post the program's output and explain what is wrong with it and show what you want the output to look like.
    print now :
    10

    want the output :
    10
    i

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: hello

    Is the second line you want for output from the println statement in the b class's max() method?
    To execute the max() method you need to
    1)create an instance of the b class
    and use that to
    2)call the max() method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: hello

    Please write new code.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: hello

    Please write new code.
    Sorry, the idea is for you to write the code and to ask us questions when you have a problem.

    Which part(s) are you having problems with?
    1) create instance of the b class
    2)calling the max() method
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jun 2012
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: hello

    i don know . i m not just getting. i did this.

    class a{
    public static void main(String args[]){
    int i=10;
    char k=max();
    System.out.println(i);
    System.out.println(k);
    }
    class b{
    public void max(){
    System.out.println("i");
    }
    }
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: hello

    Try doing the first step: create an instance of the b class.
    You will need that to be able to call any methods in the b class.


    When you get errors you need to copy and paste the full text here.

    When you post code, wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jun 2012
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: hello

    class a{
    public static void main(String args[]){
    int i=10;
    char k=max();
    char j=class b(max);
    System.out.println(i);
    System.out.println(j);
    }
    class b{
    public void max(){
    System.out.println("i");
    }
    }
    }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: hello

    Where does the code try to create a new instance of the b class?

    Take a look at the tutorial for some examples:
    Lesson: Object-Oriented Programming Concepts (The Java™ Tutorials > Learning the Java Language)

    When you get errors you need to copy and paste the full text here.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jun 2012
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: hello

    class a{
     public static void main(String args[]){ 
    	int i=10;
    	b j=new b();
    	System.out.println(i); 
    	System.out.println(j);
     
      }  
      class b{  
     
      public void max(){ 	
    	System.out.println("i"); 
      }
    }
    }
     
    using cmd. how i copy error?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: hello

    That looks like you can create an instance of the b class.

    Now try calling the b class's max() method using that new object (j) that the code creates.

    On Windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Jun 2012
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: hello

    class a{
     public static void main(String args[]){ 
    	int i=10;
    	b j=new b();
    	System.out.println(i); 
    	System.out.println(j);
     
      }  
      class b{  
     
      public void max(){ 	
    	System.out.println("i"); 
      }
    }
    }

    error :nonstatic variable this cannot be referenced from a static context b j=new b ,idk what to do. if u write code,i will be able to understand what is required.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: hello

    Please copy the exact text of the error message. I do no understand the error message you posted.
    Please copy full text of error message and paste it here. Here is a sample:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Jun 2012
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: hello

    class a{
     public static void main(String args[]){ 
    	int i=10;
    	b j=new b();
    	j.max();
    	System.out.println(i); 
    	System.out.println(j);
     
      }  
      class b{  
     
      public void max(){ 
    	System.out.println("i"); 
      }
    }
    }


    C:\Documents and Settings\dipak\Desktop>javac a.java
    a.java:4: error: non-static variable this cannot be referenced from a static con
    text
    b j=new b();
    ^
    1 error

    C:\Documents and Settings\dipak\Desktop>

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: hello

    Make the class b static by adding static before the word class:
    static class b {
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Jun 2012
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: hello

    [code=java]
    class a{
    public static void main(String args[]){
    int i=10;
    b j=new b();
    j.max();
    System.out.println(i);
    System.out.println(j);

    }
    static class b{

    public void max(){
    System.out.println("i");
    }
    }
    }
    [\code]

    C:\Documents and Settings\dipak\Desktop>javac a.java

    C:\Documents and Settings\dipak\Desktop>java a
    i
    10
    a$b@cac268

    C:\Documents and Settings\dipak\Desktop>

  18. #18
    Junior Member
    Join Date
    Jun 2012
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: hello

    class a{
    public static void main(String args[]){
    int i=10;
    b j=new b();
    j.max();
    System.out.println(i);
    System.out.println(j);
     
    }
    static class b{
     
    public void max(){
    System.out.println("i");
    }
    }
    }

    C:\Documents and Settings\dipak\Desktop>javac a.java

    C:\Documents and Settings\dipak\Desktop>java a
    i
    10
    a$b@cac268

    C:\Documents and Settings\dipak\Desktop>

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: hello

    Have you got it working now?
    If you don't understand my answer, don't ignore it, ask a question.

  20. #20
    Junior Member
    Join Date
    Jun 2012
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: hello

    yes, but i m still confused. can u explain?

  21. #21
    Junior Member
    Join Date
    Jun 2012
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: hello

    confused abt last line output

  22. #22
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: hello

    Thread moved from Member Introductions
    Please read the forum rules and in the future post questions in the most appropriate category.

  23. #23
    Junior Member
    Join Date
    Jun 2012
    Posts
    21
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: hello

    where can we select categories?

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: hello

    a$b@cac268
    That is from:
    System.out.println(j);
    It's what is returned from the default toString() method for a class. j is the class reference that was printed.
    The a$b is the name of the class. It says that b is an inner ($) class of the a class.
    The @ separates the class name from the hashcode (cac268) for the class object.
    This information can be useful later when you are debugging a larger program.
    If you don't understand my answer, don't ignore it, ask a question.

  25. The Following User Says Thank You to Norm For This Useful Post:

    dipakshah8944 (June 21st, 2012)

  26. #25
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: hello

    Go to forum and then there should be a bunch of forums. Like What's Wrong with my code? Java Theory and Questions, etc.

Page 1 of 2 12 LastLast