Solved

Issues with the scope of a variable

Posted on 2012-03-18
4
247 Views
Last Modified: 2012-08-13
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
Comment
Question by:anshuma
4 Comments
 

Assisted Solution

by:AllThingsJava
AllThingsJava earned 125 total points
ID: 37735014
//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
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 125 total points
ID: 37735016
If you want a variable this variable to be accessed outside this method also, then declare this variable as a class member variable
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 125 total points
ID: 37735075
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
 
LVL 7

Assisted Solution

by:micropc1
micropc1 earned 125 total points
ID: 37735081
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now