• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

Issues with the scope of a variable

Hi,

In the attached file I have the following function which is a void. But I want to store the max_session_state in such a way that in can be accessed globally anywhere in the code.

Or can can be a protected class member of a new class say SessionTask class but I should be able to access this from an accessor method anywhere in the code

I am hitting a dead end here. Any help ?  Also can someone point me to a really good tutorial on scope of a variable and how to handle multiple scenarios with it


thanks
-anshu



public void characters(char ch[], int start, int length) throws SAXException {
 
            
            if (max_state) {
                  
                              
                  //System.out.println("Max Session State : " + new String(ch, start, length));
                  
                  String max_session_state=new String(ch, start, length);
                  
                  System.out.println("Max Session State : " + max_session_state );
                  
                  max_state = false;
            }
java-scop.txt
0
anshuma
Asked:
anshuma
4 Solutions
 
AllThingsJavaCommented:
//Do something like:

class WhatEverClassThisThatMethodCameFrom {

private String max_session_state;

public String getMaxSessionState() {
     return max_session_state;
}

public void characters(char ch[], int start, int length) throws SAXException {
 
            if (max_state) {
                 
                             
                  //System.out.println("Max Session State : " + new String(ch, start, length));
                 
                  max_session_state=new String(ch, start, length);
                 
                  System.out.println("Max Session State : " + max_session_state );
                 
                  max_state = false;
            }
}
}
0
 
Gurvinder Pal SinghCommented:
If you want a variable this variable to be accessed outside this method also, then declare this variable as a class member variable
0
 
for_yanCommented:
Thewse aressimple tutorials about variable scope in Java


http://maiaco.com/articles/java/scope.php


http://www.java-made-easy.com/variable-scope.html

http://docs.oracle.com/javase/tutorial/java/javaOO/variables.html




>Or can can be a protected class member of a new class say SessionTask class but I should be >able to access this from an accessor method anywhere in the code

If you want to aceess your class member anywere in your code then make this member public

protected members can be accessed in the classes which are subclasses of the current class
0
 
micropc1Commented:
You might want to try doing some re-coding. Not really the best idea to put everything in main. Try something like this...  

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.helpers.DefaultHandler;
 
public class GetXmlHttp {
	
	private SAXParser saxParser;
	private SAXParserFactory factory;
	private DefaultHandler handler;
	
	private class SAXHandler extends DefaultHandler {
		
		private String max_session_state = "";
		private boolean max_state = false;
		
		public void startElement(String uri, String localName,String qName, Attributes attributes) throws SAXException {
			System.out.println("Start Element :" + qName);
		 
			if (qName.equalsIgnoreCase("max-state")) {
				max_state = true;
			}
		}
		 
		public void endElement(String uri, String localName, String qName) throws SAXException {
			System.out.println("End Element :" + qName);
		}
		 
		public void characters(char ch[], int start, int length) throws SAXException {
			if (max_state) {
				max_session_state=new String(ch, start, length);
				System.out.println("Max Session State : " + max_session_state );
				max_state = false;
			}
		}
	}
	
	public GetXmlHttp() throws ParserConfigurationException, SAXException {
		this.factory = SAXParserFactory.newInstance();
		this.saxParser = factory.newSAXParser();
	 	this.handler = new SAXHandler();	 
	}
	
	public void parse(InputSource myInputSource) throws SAXException, IOException {
		this.saxParser.parse(myInputSource, this.handler);
	}
	
	public static void main(String argv[]) throws MalformedURLException, IOException, SAXException, ParserConfigurationException {
		String url="http://mydummyur.com.test.com";
		GetXmlHttp mySaxParser = new GetXmlHttp();
		mySaxParser.parse(new InputSource(new URL(url).openStream()));
	} 
}

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now