[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Issues with the scope of a variable

Posted on 2012-03-18
4
Medium Priority
?
257 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 

Assisted Solution

by:AllThingsJava
AllThingsJava earned 500 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:
Gurvinder Pal Singh earned 500 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 500 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 500 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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

649 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