Member Variable issue

Hi,

I am calling a static method from a main method which is static.

I am having problems with DIV member variables unless i declare it as Static.

package myjsf;

public class ProgressCard {
	 static char div;

	/**
	 * @param args
	 */
	
	public static char convertGrades(int marks){
		if(marks>88)
		{
			
			div='A';
			
			
		}
		else if(marks<88)
		{
			
			div='B';
			
		}	else {
			div='c';
		
	}
		return div;
	}
	
	
	public static void main(String[] args){
		// TODO Auto-generated method stub
		//ReportCard reportCard= new ReportCard();
	  char 	mydiv=ProgressCard.convertGrades(90);
		System.out.println("first subj Div"+mydiv);
		 mydiv=ProgressCard.convertGrades(60);
		System.out.println("second subj Div"+mydiv);
		mydiv=ProgressCard.convertGrades(0);
		System.out.println("third subj Div"+mydiv);}

	
	
}

Open in new window



I tried alternately

this.div='A';

Says cannot use this in static context. I wonder why.

Best solution in this case would be to create memebr variable also as static similar to member method which is also static apart from main method?

Please advise.


Any links resources ideas highly appreciated. Thanks in advance
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ken ButtersCommented:
I think what you want to do is :

1) Remove "Static" from ConvertGrades method
2) Remove "Static" from div member variable
3) Create an instance of ProgressCard in your Main method.

public class ProgressCard {

		 char div;
		/**
		 * @param args
		 */
		public char convertGrades(int marks){
			if(marks>88)
			{
				div='A';
			}
			else if(marks<88)
			{
				div='B';
				
			}	else {
				div='c';
		}
			return div;
		}
		
		public static void main(String[] args){
			
			// TODO Auto-generated method stub
			//ReportCard reportCard= new ReportCard();
			ProgressCard myProgressCard = new ProgressCard();
			
			myProgressCard.convertGrades(90);
			
			char mydiv=myProgressCard.convertGrades(90);
			System.out.println("first subj Div"+mydiv);
			mydiv=myProgressCard.convertGrades(60);
			System.out.println("second subj Div"+mydiv);
			mydiv=myProgressCard.convertGrades(0);
			System.out.println("third subj Div"+mydiv);}
	
}

Open in new window


The way you want to think of this is... you need to create an instance of a progress card, then that will let you call the method on it.

Static means that it is not part of any instance of a class. There is one and only one.  -- which is not what you would want here.

you might want your java code to create an instance of "myProgressCard", "yourProgressCard" and "theirProgressCard" for example... each of which would have it's own DIV member variable that would each be set according to your convertGrades method.

It doesn't make sense to set a member variable of a instance of a class (in this case your "DIV" variable)... if you haven't yet created an instance of the class.

Hopefully that helps.
0
Sharon SethCommented:
this.div = 'A'

Some basic points here:

-- > 'this'  is  always a reference to the current instance .So, you can use that only with instance variables and not with static variables(div)
-->  static methods cannot not work with instance variables, but they do work with  nonstatic  local variables .
0
gudii9Author Commented:
>>Static means that it is not part of any instance of a class. There is one and only one.  -- which is not what you would want here.

I am not clear on this. How do i know it this is not what i would want or not. Since div(divison) in which a student pass based on his marks i thought should belong to individual student instance and hence instance variable but not static variable. please advise


static methods cannot not work with instance variables, but they do work with  nonstatic  local variables .


>>> but they do work with  nonstatic  local variables .


I do not have luck using non static variable from static method as well as below

package myjsf;

public class ProgressCard {
	  char div;

	/**
	 * @param args
	 */
	
	public static char convertGrades(int marks){
		if(marks>88)
		{
			
			div='A';
			
			
		}
		else if(marks<88)
		{
			
			div='B';
			
		}	else {
			div='c';
		
	}
		return div;
	}
	
	
	public static void main(String[] args){
		// TODO Auto-generated method stub
		//ReportCard reportCard= new ReportCard();
	  char 	mydiv=ProgressCard.convertGrades(90);
		System.out.println("first subj Div"+mydiv);
		 mydiv=ProgressCard.convertGrades(60);
		System.out.println("second subj Div"+mydiv);
		mydiv=ProgressCard.convertGrades(0);
		System.out.println("third subj Div"+mydiv);}

	
	
}

Open in new window


Please advise
0
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

Ken ButtersCommented:
The idea of static or instance variables/ methods s is very critical for you to understand.

Static is stuff that describes / defines / belongs to the class.  Static does NOT belong to any single instance of the class.

let's say I create an instance off ProgressCard like this:

ProgressCard someNewProgressCard = new ProgressCard();

if you looked an "someNewProgressCard" in memory... what would you see?

You would see an object called someNewProgressCard of type ProgressCard, and you would see space allocated for it's member variable... div.  Same thing if you created an additional instance of the class .... say pg2.

someNewProgessCard
--- div (belongs to this instance.... someNewProgressCard)

pg2
--- div (belongs to this instance... pg2)

There is a separate div variable created for each instance of the class.  If you don't create an instance... then the div instance variable is not created either.

When you specify Static... then that applies to the ENTIRE CLASS.  When you use a static method, you have to think... this has to work... even if I don't create an instance of the class.

so... in your static method.... you referred to a non-static variable div... that belongs to some instance of ProgressCard.

1) First thing is... there might not even be an instance of ProgressCard created when you call the static method

2) Second thing is... even if there was an instance (or 2 or 3 or more..) of ProgressCard created, then the static method would have no way of knowing which INSTANCE of div you are referring to.

you can invoke the static method by just using the class name.
ProgressCard.convertGrades

But if you remove the static modifier from the method convertGrades, then you have to invoke the method from a particular instance of ProgressCard... like so:
someNewProgressCard.convertGrades

That is why you are having trouble referencing div (a non-static variable) from a static method.
0
gudii9Author Commented:
>>so... in your static method.... you referred to a non-static variable div... that belongs to some instance of ProgressCard.



public static char convertGrades(int marks){
            if(marks>88)
            {
                  
                  div='A';
                  
                  
            }


In my sttic method i referred non static variable 'div' as above.

i have neither called div on class name(ProgressCard.div) nor called on instance name(pg1.div).


In this case what compiler thinks. does it thinks i am calling non static 'div' variable using class or instance or something else..
Based on eclipse compilation error says ' cannot make static reference to non static field div'

why compiler concluded that i am making static reference(i did not call on on class name--->ProgressCard.div)
Please advise
0
Ken ButtersCommented:
You have to think of static methods/properties as something separate from non-static methods/properties.

Let's say that you defined in your class ProgressCard a static variable called divStatic.

The fully qualified name of the variable could be referred to as ProgressCard.divStatic.

Note... that you would not have to create an instance of the class to refer to divStatic, because divStatic by being defined as static belongs to the class.

Now... completely separate from the class, is an instance of a class.  Let's say I have a non-static variable called div.  In order for 'div' to even exist, I must first declare an instance of the class.  Let's say I declare an instance of Progress card and call that instance 'item1'.

ProgressClass item1 = new ProgressClass();

Since 'div' was declared as non-static, that means that the instance... item1 has inside it a variable called 'div'.

div can ONLY be referred to from the viewpoint of that particular instance.

so in this case, we would refer to it as "item1.div".   (unless you declared it private in which case you would need a getter/setter to get to it).

When you are in a static method of the ProgressCard class, that static method knows absolutely nothing about "item1".  Since it is unaware of "item1", there is no way for it to know anything about "div" which is inside "item1".

The only thing static methods can "SEE" are static member variables.

So when you refer to "div" within a static method, the compiler knows that you are trying to refer to something that does not exist as far as that static method is concerned.  

The Static variable and static method exist, even if the there has never been an instance created.  

Non-Static variables will not be carved out of memory, and will not exist, until you create a new instance of that class.

why compiler concluded that i am making static reference(i did not call on on class name--->ProgressCard.div)
Please advise

You did in fact refer to ProgressCard.div, because of the fact that you used "div" in your static method.  Any use a variable in a static method will implicitly have the class name in front of it.

You can think of it this way.. since you used "div" inside your static method, the compiler internally will change it to "ProgressCard.div" for you.   And the compiler knows that there is no static variable "div" defined, therefore it will complain.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.