How To Get A Variable From Constructor Into Another Method
Nov 11, 2014
I am having an issue with a small part of my project. i am supposed to make a hash table with a file containing words. The file is being passed into my constructor, where i basically just all it "filename" of type string. Im supposed to get the contents of the file and put it into the table. the problem I'm having is making the connection between my constructor and a method called "start" which basically does all the work. Im not sure how to go by doing this, how could i use the variable "filename" from my constructer in my start method?:
import java.io.*;
import java.util.*;
public class WordCount {
//private fields, including your HashMap variable
HashMap<String,Object> hmap =new HashMap<String,Object>();//(table size,load factor)
public WordCount( String infileName){
String filename =infileName;
So, this is weird for me because I don't really understand why the BorderLayout class constructor is being initialized as a parameter for the setLayout..
public Polygon polygonFrom(Point[] corners) { // method body goes here }
So from what I understand this is a constructor method for a Polygon object from the Polygon class. What I dont get is the name of the method polygonFrom()
Shouldn't a constructor for a Polygon just have the same name as the class? Because from earlier examples in the tutorial it seems to me that this is what has been done
For example:
public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; }
I don't understand, why when in the constructor of the superclass A the method init() is called (line 4), the overridden version of the subclass is invoked (line 26) and not the superclass version (line 7):
class A { public A () { init(); } public void init() { System.out.println("test");
[Code] ....
I would have guessed that above code prints
test 1
But instead you get a NPE (because when the constructor of B is invoked
public static void main(String[] args) { new B(); }
Then there is first the implicit call to super:
public B() { s = " "; init(); }
Which is the constructor of A:
public A () { init(); }
But here now this init() method is not the version of A ( public void init() { System.out.println("test"); }) but the overriden version of the subclass (B): public void init() { System.out.println(s+=s.length()); }...
Which throws an NPE of course, because the initialization of s has not occured yet (it happens only after the implicit call to super() has finished (see public B() { s = " "; init(); }))
1 - I don't understand why I'm getting an empty stack error when calling the removecard method in my Table class line 13?
2 - I am trying to use a constructor to populate my deck of cards instead of a method when creating my theDeck object in my Table class line 11 but I get the following error:
java.lang.StackOverflowError at java.util.Vector.<init>(Unknown Source) at java.util.Vector.<init>(Unknown Source) at java.util.Stack.<init>(Unknown Source) at Deck.<init>(Deck.java:7) at Deck.<init>(Deck.java:34)
My code has a method where the users input a bunch of variables and then those variables get added together in a new variable. What I want to know is how do I call the variable that is in the other method to another method?
import java.util.*; public class Calc{ public static void main (String [] args){ determinevalue1(); determinevalue2(); determineTotalvalue(double value1, double value2);
If I have a method that creates a new variable (the name of which is a string argument for the method). Can I get that variable to appear in the workspace in the variables pane?
So instead of just using return to output the variable how do I get in that variables pane?
Say I have two classes, Author and Book, and I have 2 author objects and 10 book objects. I would like to know how to do two things:
1) Make some sort of connection that makes clear that author X wrote books A, B and F. 2) Call a method from a book object that is connected to an author.
Seeing as I don't know which books will be connected to an author, is there some way to call a method of an object bases on a variable object name? Something like call the getFirstPage() method for every book that is linked to author X?
public Unit(String code, String name) { enrolStudent(student); this.unitCode = code; this.unitName = name; } public void enrolStudent(Student newStudent){ students = new ArrayList<Student>(); newStudent = new Student(24662496, "Kingsley", " Iwunze"); students.add(newStudent); }
how can I call this enrolStudent() method on this Unit constructor in another class when I create a new Unit. all I need is to enroll students in units when units are created. below is my create unit method.
this code won't compile because selected row must be declared as final because of it being defined outside the window listener. Is their anyway around this? If I make it final the first time that the variable is called it keeps it starting value until the GUI is closed.
butEdit.addActionListener (new ActionListener () { @Override public void actionPerformed (java.awt.event.ActionEvent evt) { int selectedRow = table.getSelectedRow (); final String [] values = custTableModel.getRowValues (selectedRow);
Created a java.sql.connection object. Refering those obj inside public void run() { } If i declare as final inside a method, i can't refer those outside method due to scope. Cannot refer to a non-final variable dbConnObj inside an inner class defined in a different method...
The term "Local variable" is related to scope. That is a local variable is one which is defined in a certain block of code, and its scope is confined inside that block of code.And a "Member variable" is simple an instance variable.
I read in a discussion forum that when local variables are declared (example code below), their name reservation takes place in memory but they are not automatically initialized to anything. On the other hand, when member variables are declared, they are automatically initialized to null by default.
Java Code: public void myFunction () { int [] myInt; // A local, member variable (because "static" keyword is not there) declared } mh_sh_highlight_all('java');
So it seems that they are comparing local variables and member variables. While I think a member variable can also be be local in a block of code, isn't it?
when we create another variable and set it equal to the first : Car c2 = c1;
we're pointing c2 at the same car object that c1 points to (as opposed to pointing c2 at c1, which in turn points at the car). So if we have code like,
Car c1 = new Car(); Car[] cA = {c1, c1, c1, c1};
are we doing the same? Are we creating four *new* reference variables, each of which points at the same car (again as opposed to pointing them at c1 itself)? I think so, but want to make sure I'm understanding this correctly.
I have a JFrame jf and JPanel jp on it. jp has five TextFields named cel1, cel2.. cel5. I wish to construct a String Cel + for loop index and run a for loop to reset the values of all the text fields using a single statement such as cel1.SetText("abc"). Similar things can be done in foxfro. How does one do it in java?
I was practicing my java skills and came across an exercise in which a non parameter constructor calls a two parameter constructor. I tried a few searches online but they all came back unsuccessful. This is the part I am working on:
public PairOfDice(int val1, int val2) { // Constructor. Creates a pair of dice that // are initially showing the values val1 and val2. die1 = val1; // Assign specified values die2 = val2; // to the instance variables. } public PairOfDice() { // Constructor that calls two parameter constructor }
I tried calling the two constructor using the line "this(val1, val2)" but I get an error because val1 and val2 are local variables.
Then I tried to use the same signature: "this(int val1, int val2)" but that didn't work either.
I'm wonder about the issue of constructor for arrays. Let say I have a class tablica, and one component int[] tab. If I get it right until now tab is nothing more than empty reference to some unexisting array?
import java.util.Random; class tablica{ int[] tab; tablica (){ // i wish it was constructor
[code]....
Then, I'm trying to build the constructor for class tablica. What can be the parameter of such constructor? Is it fields of array? It is simple forf for basic variable
- I liken values defined in constructor with those global defined in class. But how to do it with array component tab.
If I create array object in main method then how can I use this constructor?
public class TestClass { public TestClass(String k){System.out.println(k);} public static void main(String[] args) { try { hello(); } catch(Exception e){System.out.println(e);}
[Code] ....
Explain how to catch block act as constructor with parameter?