Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes scope of variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "scope of variable" Watch "scope of variable" New topic
Author

scope of variable

meeraa pillaii
Ranch Hand

Joined: Apr 19, 2008
Posts: 30
Hi,
I have defined a variable as public in my program. But this after assigning value from a function call, is not retaining the value after the scope of that function.

Kindly help as this is quite urgent.

Regards
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Some actual code would help. There could be several reasons why this is happening, almost certainly mistakes in the source code.


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
meeraa pillaii
Ranch Hand

Joined: Apr 19, 2008
Posts: 30
Here is the piece of program, since original program is big.

public class printsecondpage {
//this variable 'MoreInd' having the problem
boolean MoreInd;
public static void main(String[] args) {

printsecondpage example = new printsecondpage();
System.exit(0);
}
public printsecondpage() {
PrinterJob printJob = PrinterJob.getPrinterJob();
Book book = new Book();
book.append(new IntroPage1(), printJob.defaultPage());
if (MoreInd ){
System.out.println("True " );
}
}
private class IntroPage1 implements Printable {
public int print(Graphics g, PageFormat pageFormat, int page) {
MoreInd = true;
}
}

I am just wondering why "true" is not displaying even after setting it inside the function as TRUE.

Please note this is defined as a public.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

I am just wondering why "true" is not displaying even after setting it inside the function as TRUE.


This is speculation, as the code that you provided doesn't compile, so hence, we can't test it.... but do you think it has something to do with the System.exit() method call? Basically, you are exitiing the application before the print streams can actually flush.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
meeraa pillaii
Ranch Hand

Joined: Apr 19, 2008
Posts: 30
Hi Henry,

This is only part of the program. I have mentioned the relevant lines only.
Kindly help on how to declare variable to retain values throughout the program.

regards,
meeraa pillaii
Ranch Hand

Joined: Apr 19, 2008
Posts: 30
Hi Henry,

This is only part of the program. I have mentioned the relevant lines only.
Kindly help on how to declare variable to retain values throughout the program.

regards,
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

Kindly help on how to declare variable to retain values throughout the program.


Well, can you confirm whether my speculation is correct or not? For all we know, the values were retained -- and your program exited before the value were printed.

Henry
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Please put your code in code tags next time, for formatting reasons. My big question is why you think this code should print true? As far as I can see, you never invoke the print(Graphics,PageFormat,int) method, and since MoreInd is false by default on declaration as an instance variable, it will remain false.

It may just be that you omitted that section of code, but I don't think your method is ever being called in the first place...
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
I don't see anywhere in the code provided where IntroPage1.print() is called. Does the Book.append() method call print()? And what is the reasoning behind the System.exit() call?


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
meeraa pillaii
Ranch Hand

Joined: Apr 19, 2008
Posts: 30
Hi,
My program function calls IntroPage1, which is defined as

public class IntroPage1 implements Printable {
public int print(Graphics g, PageFormat pageFormat, int page) {

These are used to invoke the printer. But I am not getting the values set inside these, in the main program.

Can any body suggest a way.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

My program function calls IntroPage1


IntroPage1 is a class -- not a method. You still need to call the method, which is not something that you have shown.

Henry
meeraa pillaii
Ranch Hand

Joined: Apr 19, 2008
Posts: 30
ok...
public class printsecondpage {
boolean MoreInd;
public class printsecondpage {
public static void main(String[] args) {
printsecondpage example = new printsecondpage();
System.exit(0);
}
public printsecondpage() {
PrinterJob printJob = PrinterJob.getPrinterJob();
Book book = new Book();
TotRecords = GetRecordCount();
book.append(new IntroPage1(), printJob.defaultPage());

if (MoreInd ){
book.append(new IntroPage2(), printJob.defaultPage());
TotRemPages = TotRemPages - 1;
}
}

printJob.setPageable(book);
if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception PrintException) {
PrintException.printStackTrace();
}
}
}
public class IntroPage1 implements Printable {
public int print(Graphics g, PageFormat pageFormat, int page) {
if (ypos > 570){
MoreInd = true;
g.drawString(strSUM_AMOUNT, 450, ypos);
g.drawString(strPricePercent, 450, ypos + 15);
g.drawString("994,500.00", 450, ypos + 30);
return PAGE_EXISTS;
}}

Here MoreInd is the variable i declared and changed when ypos > 570.when i try this constructor the value is not changed.Please help me..Today i should submit this work

Regards
If you send the mail id i will send the program
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Please use code tags when posting code. It makes it much easier to read and reason about what's happening.

I copy/pasted your code into an editor and formatted it. It makes no sense to me as written, there's no way it will compile. Is this really the code you're using?

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
And even if it did compile and make sense, you still haven't shown where IntroPage1.print() is called.
meeraa pillaii
Ranch Hand

Joined: Apr 19, 2008
Posts: 30
Hi Friends
It is long program so i just copy some of line and posted the code
here is the program.just run

package com.backup;

import java.awt.Graphics;
import java.awt.print.Book;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;

public class test {
//this firstPage variable is problem one which is not changing
boolean firstPage = true;
boolean MoreInd = true;

int ypos = 370;

public static void main(String[] args) {

test example = new test();
System.exit(0);
}

public test() {
PrinterJob printJob = PrinterJob.getPrinterJob();
Book book = new Book();
book.append(new IntroPage1(), printJob.defaultPage());

//SOP fpr firstPage
if (firstPage == false)
System.out.println("first page is false");

if (firstPage)
System.out.println("first page is true");

printJob.setPageable(book);
if (printJob.printDialog()) {
try {
printJob.print();
} catch (Exception PrintException) {
PrintException.printStackTrace();
}
}
}

private class IntroPage1 implements Printable {

public int print(Graphics g, PageFormat pageFormat, int page) {
ypos = 590;
if (ypos > 570) {

MoreInd = true;
// Here ypos is 590 so i am changing firstPAge value to falseBut
// not reflecting on SOP above
firstPage = false;

g.drawString("hhhhhhh", 100, 100);

}
return PAGE_EXISTS;

}

}

}

once again a big thanks
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
The IntroPage1.print() method hasn't been called when you test the firstPage variable. Try this:
The method isn't called until the printJob.print() is called. This has nothing to do with the scope of any variables.
 
wood burning stoves
 
subject: scope of variable
 
Similar Threads
how to retrieve javaScript variable into jsp
Understanding Javascript Closures
Variables in JSP
Regarding gc
Passing Argument To JavaScript Method