Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

scope of variable

 
meeraa pillaii
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some actual code would help. There could be several reasons why this is happening, almost certainly mistakes in the source code.
 
meeraa pillaii
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20898
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
meeraa pillaii
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20898
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
meeraa pillaii
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20898
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And even if it did compile and make sense, you still haven't shown where IntroPage1.print() is called.
 
meeraa pillaii
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic