Results 1 to 6 of 6
  1. #1
    vknehra10 is offline Member
    Join Date
    Dec 2016
    Posts
    99
    Rep Power
    0

    Default Variable is not invoked via refernce variable

    Java Code:
    public class fine{
    	
    	public void show(String c)
    		{
    			String v=c;
    		    System.out.println(v);
    		}
    	
    	
    		
    		
    		  
    	public static void main(String Dev[]){
    		
    		
    		StringBuffer s=new StringBuffer("vivek");
    		StringBuffer s1=new StringBuffer("Dev");
    		
    		/**if(s.equals(v))
    			System.out.println("true");
    		else
    			System.out.println("false");
    		**/
    		fine f=new fine();
    		f.show("vivek");
    		System.out.println(s+" "+s1);
    		
    	}
    }
    i want to call String v but i can't am i doing some mistake in this code..

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,532
    Rep Power
    24

    Default Re: Variable is not invoked via refernce variable

    The variable 'v' only exists during the execution of the 'show' method.
    Specifically it only exists at line 5 (where it is declared) and 6 (where the method returns).
    You can't access it outside that method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Variable is not invoked via refernce variable

    is there anyway to compare with variable 'v'

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,532
    Rep Power
    24

    Default Re: Variable is not invoked via refernce variable

    You could make 'v' an instance variable.
    That is, declare it outside of the method:
    Java Code:
    private String v;
    
    public void show(String c) {
    etc etc.
    }
    but you might need to explain what you are trying to achieve?
    The variable names and method names don't tell us much.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,144
    Rep Power
    12

    Default Re: Variable is not invoked via refernce variable

    I also wonder why you are using StringBuffer? It is normally used to manipulate an internal character array since
    Strings are immutable. If you still want to do that I would recommend using StringBuilder. You can check the API
    on each class to see why. However, if you just want to create a string and assign a value to it, just do:

    String a = "this is a string";
    String b = "this is another string";

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,939
    Rep Power
    7

    Default Re: Variable is not invoked via refernce variable

    Quote Originally Posted by jim829 View Post
    I also wonder why you are using StringBuffer?
    Here's why:
    - Because this is the internet. It is filled to the brim with legacy shit that we never get rid of.
    - Today, people are STILL posting code examples using StringBuffer. Crappy tutorials are everywhere.
    - Even @Java retweeted a tutorial about using StringBuffers ON JUNE 8th! Unfortunately, they deleted the tweet after I (and likely many others) gave them flak about it. (See my reply: https://twitter.com/NLSurfMan/status/872820499772055552)
    - People not reading the javadoc that specifically says to use StringBuilder

    We will be forever stuck with Hashtables, StringBuffers, Vectors until Java does no longer exist.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. local variable and instance variable
    By dhia annur in forum New To Java
    Replies: 7
    Last Post: 12-24-2013, 05:01 AM
  2. Replies: 2
    Last Post: 11-02-2013, 09:53 AM
  3. Replies: 2
    Last Post: 01-09-2013, 09:10 PM
  4. Dynamic variable name based on other variable
    By nadissen in forum Eclipse
    Replies: 4
    Last Post: 05-06-2011, 07:22 PM
  5. Replies: 11
    Last Post: 07-07-2010, 07:02 AM

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
  •