Results 1 to 17 of 17
Thread: Static variable context Errors ?
- 08-06-2008, 06:38 PM #1
Member
- Join Date
- Jun 2008
- Posts
- 48
- Rep Power
- 0
- 08-06-2008, 08:12 PM #2
Could you write a 10 line program that shows these errors and we'll explain.
- 08-06-2008, 08:24 PM #3
Member
- Join Date
- Aug 2008
- Posts
- 1
- Rep Power
- 0
Hi
can any one help me how to load CSv file to table using java .
- 08-08-2008, 11:57 AM #4
Member
- Join Date
- Jun 2008
- Posts
- 48
- Rep Power
- 0
Norm i will ask about the 1st problem later .. pls give a look to this one -->
class exshyone {
int a;
exshyone() {
a=10;
System.out.println(a);
}
}
class shyone extends exshyone {
a=20;
System.out.println(a);
public static void main(String []ar) {
shyone one =new shyone();
}
}
Errors-->
D:\jdk1.5.0_06\bin\shyone.java:11: <identifier> expected
a=20;
^
D:\jdk1.5.0_06\bin\shyone.java:12: <identifier> expected
System.out.println(a);
^
2 errors
Questions -->
1. Can't I access variable 'a' of parent in child like the above prog.
2. why we require the construct to declare & initialize the variable in parent.
- 08-08-2008, 03:42 PM #5
Program statements like a=20; must be inside a method or constructor.
The compiler isn't real smart and easily gets confused if you don't follow the rules.why we require
- 08-08-2008, 04:45 PM #6
Member
- Join Date
- Jun 2008
- Posts
- 48
- Rep Power
- 0
but if i will put int 'a' in a constructor or function it will become local for that only. that means i cannot access that class level variable in the child class.
- 08-08-2008, 06:12 PM #7
No, it means that you have hidden the 'a' in the Constructor. The parent's 'a' is still visible to other methods. Modify your above program to see.
- 08-08-2008, 06:23 PM #8
Member
- Join Date
- Jun 2008
- Posts
- 48
- Rep Power
- 0
wats the probl. in previous code ?
Norm, i m able to access the variable declared in parent class in child one. here is the code-->
class Parent
{
int d=100;
void f1()
{
System.out.println("IN PARENT f1");
}
}
class Child1 extends Parent
{
void f2()
{
System.out.println("value of D"+d); // Here i m accessing the variable D of Class levl. var. of Parent
System.out.println("in Child1 f2");
// Child1 ob1=new Child2();
// ob1.f1();
}
}
/*
void f1() {
System.out.println("in Child1 f1");
}
}
*/
public class Child2 extends Child1
{
/*
void f2() {
System.out.println("In Child2 f2");
Child2 ob1=new Child2();
ob1.f1();
}
void f1() {
System.out.println("In Child2 f1");
}
*/
public static void main(String[] args)
{
Parent p=new Parent();
p.f1();
Child1 ob=new Child1();
ob.f2();
/* Child1 ch1=new Child1();
ch1.f2(); */
}
}
then why i m not able to do that in the previous code... reasons
- 08-08-2008, 06:28 PM #9
Can you be more specific?i m not able to do that in the previous code
what does do that mean? What variables in what methods/classes?
- 08-08-2008, 07:03 PM #10
Member
- Join Date
- Jun 2008
- Posts
- 48
- Rep Power
- 0
- 08-08-2008, 07:04 PM #11
Member
- Join Date
- Jun 2008
- Posts
- 48
- Rep Power
- 0
wats the probl. in previous code ?
Norm, i m able to access the variable declared in parent class in child one. here is the code-->
class Parent
{
int d=100;
void f1()
{
System.out.println("IN PARENT f1");
}
}
class Child1 extends Parent
{
void f2()
{
System.out.println("value of D"+d); // Here i m accessing the variable D of Class levl. var. of Parent
System.out.println("in Child1 f2");
// Child1 ob1=new Child2();
// ob1.f1();
}
}
/*
void f1() {
System.out.println("in Child1 f1");
}
}
*/
public class Child2 extends Child1
{
/*
void f2() {
System.out.println("In Child2 f2");
Child2 ob1=new Child2();
ob1.f1();
}
void f1() {
System.out.println("In Child2 f1");
}
*/
public static void main(String[] args)
{
Parent p=new Parent();
p.f1();
Child1 ob=new Child1();
ob.f2();
/* Child1 ch1=new Child1();
ch1.f2(); */
}
}
then why i m not able to do that in the previous code... reasons
- 08-08-2008, 07:23 PM #12
Member
- Join Date
- Jun 2008
- Posts
- 48
- Rep Power
- 0
I hav tried the code 1 -->
class exshyone {
int a=100;
exshyone() {
a=10;
System.out.println(a);
}
}
class shyone extends exshyone {
void show() {
a=290;
System.out.println(a);
}
public static void main(String []ar) {
shyone one =new shyone();
one.show();
}
}
i m able to access var. ' a ' When i put
a=290;
System.out.println(a);
in any Function or in Cunstructor .
cant we execute the code like this -->
class exshyone {
int a=100;
exshyone() {
a=10;
System.out.println(a);
}
}
class shyone extends exshyone {
//void show() {
a=290;
System.out.println(a);
// }
public static void main(String []ar) {
shyone one =new shyone();
one.show();
}
}
if not wat is the reason java dont permit this code.
- 08-08-2008, 08:09 PM #13
You don't say what the problem is?
If you get errors, please copy them here.java dont permit this code.
- 08-08-2008, 08:30 PM #14
Member
- Join Date
- Jun 2008
- Posts
- 48
- Rep Power
- 0
wats the probl. in previous code ?
Norm, i m able to access the variable declared in parent class in child one. here is the code-->
class Parent
{
int d=100;
void f1()
{
System.out.println("IN PARENT f1");
}
}
class Child1 extends Parent
{
void f2()
{
System.out.println("value of D"+d); // Here i m accessing the variable D of Class levl. var. of Parent
System.out.println("in Child1 f2");
// Child1 ob1=new Child2();
// ob1.f1();
}
}
/*
void f1() {
System.out.println("in Child1 f1");
}
}
*/
public class Child2 extends Child1
{
/*
void f2() {
System.out.println("In Child2 f2");
Child2 ob1=new Child2();
ob1.f1();
}
void f1() {
System.out.println("In Child2 f1");
}
*/
public static void main(String[] args)
{
Parent p=new Parent();
p.f1();
Child1 ob=new Child1();
ob.f2();
/* Child1 ch1=new Child1();
ch1.f2(); */
}
}
then why i m not able to do that in the previous code... reasons
- 08-08-2008, 09:00 PM #15
Member
- Join Date
- Jun 2008
- Posts
- 48
- Rep Power
- 0
wats the probl. in previous code ?
Norm, i m able to access the variable declared in parent class in child one. here is the code-->
class Parent
{
int d=100;
void f1()
{
System.out.println("IN PARENT f1");
}
}
class Child1 extends Parent
{
void f2()
{
System.out.println("value of D"+d); // Here i m accessing the variable D of Class levl. var. of Parent
System.out.println("in Child1 f2");
// Child1 ob1=new Child2();
// ob1.f1();
}
}
/*
void f1() {
System.out.println("in Child1 f1");
}
}
*/
public class Child2 extends Child1
{
/*
void f2() {
System.out.println("In Child2 f2");
Child2 ob1=new Child2();
ob1.f1();
}
void f1() {
System.out.println("In Child2 f1");
}
*/
public static void main(String[] args)
{
Parent p=new Parent();
p.f1();
Child1 ob=new Child1();
ob.f2();
/* Child1 ch1=new Child1();
ch1.f2(); */
}
}
then why i m not able to do that in the previous code... reasons
- 08-08-2008, 09:07 PM #16
Member
- Join Date
- Jun 2008
- Posts
- 48
- Rep Power
- 0
if i write like this (without function or Cunstructor ) -->
class exshyone {
int a=100;
exshyone() {
a=10;
System.out.println(a);
}
}
class shyone extends exshyone {
//void show() {
a=290; // Here (like this)
System.out.println(a);
// }
public static void main(String []ar) {
shyone one =new shyone();
one.show();
}
}
Errors-->
D:\jdk1.5.0_06\bin\shyone.java:15: <identifier> expected
a=290;
^
D:\jdk1.5.0_06\bin\shyone.java:16: <identifier> expected
System.out.println(a);
^
2 errors
- 08-08-2008, 09:11 PM #17
Member
- Join Date
- Jun 2008
- Posts
- 48
- Rep Power
- 0
wats the probl. in previous code ?
Norm, i m able to access the variable declared in parent class in child one. here is the code-->
class Parent
{
int d=100;
void f1()
{
System.out.println("IN PARENT f1");
}
}
class Child1 extends Parent
{
void f2()
{
System.out.println("value of D"+d); // Here i m accessing the variable D of Class levl. var. of Parent
System.out.println("in Child1 f2");
// Child1 ob1=new Child2();
// ob1.f1();
}
}
/*
void f1() {
System.out.println("in Child1 f1");
}
}
*/
public class Child2 extends Child1
{
/*
void f2() {
System.out.println("In Child2 f2");
Child2 ob1=new Child2();
ob1.f1();
}
void f1() {
System.out.println("In Child2 f1");
}
*/
public static void main(String[] args)
{
Parent p=new Parent();
p.f1();
Child1 ob=new Child1();
ob.f2();
/* Child1 ch1=new Child1();
ch1.f2(); */
}
}
then why i m not able to do that in the previous code... reasons
Similar Threads
-
non-static member can not be referenced from a static context
By christina in forum New To JavaReplies: 3Last Post: 03-20-2009, 12:35 AM -
About static variable
By MarkWilson in forum New To JavaReplies: 5Last Post: 06-27-2008, 01:43 PM -
Non-Static method in static context error
By wizmang in forum New To JavaReplies: 4Last Post: 04-24-2008, 08:51 AM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks