Results 1 to 7 of 7
  1. #1
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default I'm new in java and doing Practice of Static i need help !!

    i'm doing practice of static methods and static members so i am unable to pass value and call the methods please help

    Thanks

    Java Code:
    class stc
    {
    	int x;
    	static String name;
    	static class stc2{
    		
    	
    	public static void fun(String n)
    	{
    		name=n;
    	}
    	}
    }
    
    public class sample
    {
    	public static void main(String[] args)
    	{
    		stc s = new stc();
    		fun("vivek");
    		System.out.println(stc.stc2.name);
    	    
    	}
    	
    }
    Last edited by JosAH; 12-03-2016 at 11:30 AM. Reason: fixed the [code] ... [/code] tags

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: I'm new in java and doing Practice of Static i need help !!

    Class stc has a statically nested class stc2 which has a static member method fun; the outer class stc als has a static member name. So you can refer/call them as:

    Java Code:
    stc.stc2.fun("vivek");
    stc.name;
    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default Re: I'm new in java and doing Practice of Static i need help !!

    thank you so much josah and if i make a method in static inner class for return value. then how to call...

    thanks

    Java Code:
     class stc
    {
    	int x;
    	static String name;
    	static class stc2{
    		
    	
    	public static void fun(String n)
    	{
    		name=n;
    	}
    	static void  String get()
    	{
    		return(name);
    	}
    	}
    }
    
    public class sample
    {
    	public static void main(String[] args)
    	{
    		stc s = new stc();
    		stc.stc2.fun("vivek");
    		System.out.println(stc.stc2.get());
    	    
    	}
    	
    }
    Last edited by Norm; 12-03-2016 at 06:26 PM. Reason: Changed code tags to use [] vs <>

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: I'm new in java and doing Practice of Static i need help !!

    If you are getting compiler errors, please copy the full text and paste it here.

    Note: Java naming conventions say to give classes names that start with uppercase letters.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default Re: I'm new in java and doing Practice of Static i need help !!

    Quote Originally Posted by Norm View Post
    If you are getting compiler errors, please copy the full text and paste it here.

    Note: Java naming conventions say to give classes names that start with uppercase letters.
    class stc
    {
    int x;
    static String name;
    static class stc2{


    public static void fun(String n)
    {
    name=n;
    }
    static void String get()
    {
    return(name);
    }
    }
    }

    public class sample
    {
    public static void main(String[] args)
    {
    stc s = new stc();
    stc.stc2.fun("vivek");
    System.out.println(stc.stc2.get());

    }

    }

    Result:-
    sample.java:12: error: '(' expected
    static void String get()
    ^
    1 error

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: I'm new in java and doing Practice of Static i need help !!

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    static void String get()
    A method can not be defined as void and also to return a String. It must be only one of those.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    vknehra10 is offline Senior Member
    Join Date
    Dec 2016
    Posts
    103
    Rep Power
    0

    Default Re: I'm new in java and doing Practice of Static i need help !!

    Thanks Norm I keep it in my mind from next time
    Java Code:
      This
    and thnks again to remind me that void or string is different and i'm new in java soo these mistakes are happening in strting andd.... im also new member in this forum ...norm tell me a thing how to learn more and practice in java

    thanks....

Similar Threads

  1. Replies: 8
    Last Post: 11-19-2014, 08:58 PM
  2. Replies: 1
    Last Post: 10-25-2012, 01:49 AM
  3. Java SOAP - Best Practice
    By Jeeni101 in forum Advanced Java
    Replies: 16
    Last Post: 07-27-2011, 09:42 AM
  4. I need help on a practice code for java
    By Sageinquisitor in forum New To Java
    Replies: 12
    Last Post: 07-13-2010, 04:00 PM
  5. java practice
    By miko5054 in forum New To Java
    Replies: 3
    Last Post: 03-22-2010, 05:31 PM

Tags for this Thread

Posting Permissions

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